首页 > 其他分享 >牛客练习赛121补题

牛客练习赛121补题

时间:2024-01-27 17:01:18浏览次数:29  
标签:练习赛 const int back i64 牛客 补题 lcm dp

C.

思路

由于水滴会影响一个区间里的水滴,所以只需要为何区间[l, r]即可

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 1e18;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

int a[N];
int n, p, l, r;

void dfs(int x) {//dfs(x)表示在x的位置上加1水滴
    a[x] ++;
    if (x == 0 || x == n + 1) return;
    if (a[x] == 10) {
        if (x == l) l --;
        if (x == r) r ++;
        dfs(r);
        dfs(l);
    }
}

void solve() {
    cin >> n >> p;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    a[0] = a[n + 1] = 0;
    l = p - 1;
    r = p + 1;
    dfs(p);
    
    cout << a[0] << ' ' << a[n + 1] << endl;
}   

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

D.

思路

构造。先用操作1构造出两个gcd(x, y), 然后用倍增是思路把逼近lcm(x, y)的规模,在达到lcm(x, y)的规模时用位运算的方式逼近正确答案

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 1e18;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;

void solve() {
    i64 x, y;
    cin >> x >> y;
    if (x < y) swap(x, y);
    i64 d = __gcd(x, y);
    i64 lcm = x / d * y;
    
    if (x == lcm || y == lcm) {
        cout << "0\n";
        return;
    }
    
    vector<i64> t;
    vector<pair<i64, i64>> op;
    op.push_back({x, y});
    op.push_back({x, y});
    t.push_back(d);
    
    while (1) {
        if (lcm - t.back() < t.back()) break;
        i64 tmp = t.back();
        op.push_back({tmp, tmp});
        t.push_back(tmp + tmp);
        if (lcm - t.back() < t.back()) break;
        op.push_back({tmp, tmp});
    }
    
    if (t.back() != lcm) {
        i64 cnt = 1;
        i64 b = (lcm - t.back()) / d;
        while (b) {
            if (b & 1) {
                i64 tt = cnt * d;
                op.push_back({t.back(), tt});
                i64 z = t.back() + tt;
                t.push_back(z);
            }
            b >>= 1;
            cnt *= 2;
        }
    }
    
    cout << op.size() << endl;
    for (int i = 0; i < op.size(); i++) {
        if (i < 2) cout << 1 << ' ';
        else cout << 2 << ' ';
        cout << op[i].first << ' ' << op[i].second << endl;
    }
}   

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

E.

思路

折扣券和立减券要用在大价格的物品上,dp[i][j]表示前i个物品都使用了券且用了j张折扣券

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 1e18;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;

void solve() {
    int n, a, b;
    cin >> n;
    vector<i64> p(n);
    for (auto &i : p) cin >> i;
    cin >> a;
    vector<i64> x(a);
    for (auto &i : x) cin >> i;
    cin >> b;
    vector<i64> y(b);
    for (auto &i : y) cin >> i;
    sort(p.begin(), p.end(), greater<i64>());
    sort(x.begin(), x.end());
    sort(y.begin(), y.end(), greater<i64>());
    
    double ans = 0;
    for (int i = a + b; i < n; i++)
        ans += 1.0 * p[i];
    
    double dp[5010][5010];
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            dp[i][j] = 1e9;
    dp[0][0] = 0;
    
    n = min(n, a + b);
    
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= min(i, a); j++) {
            if (i - j > b) continue;
            if (j > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 0.01 * p[i - 1] * x[j - 1]);
            if (i - j > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j] + max(0.0, 1.0 * p[i - 1] - 1.0 * y[i - j - 1]));
        }
    }

    double mn = 1e9;
    for (int j = 0; j <= a; j++) mn = min(mn, dp[n][j]);
    ans += mn;
    printf("%.10lf\n", ans);

}   

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    //cout.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

标签:练习赛,const,int,back,i64,牛客,补题,lcm,dp
From: https://www.cnblogs.com/kichuan/p/17991653

相关文章

  • 牛客练习赛121
    牛客练习赛121出题人题解|牛客练习赛121题解小念吹气球解题思路:字符长度加字符种类数。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingi128=__int128_t;voidso......
  • 牛客周赛 Round 29
    牛客周赛Round29小红大战小紫代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingi128=__int128_t;voidsolve(){inta,b;cin>>a>>b;......
  • 2023.6.3(Mon.) 练习赛总结
    T1分层图跑最短路。为了优化空间,用了隐式连边的方法。T2dp,主要的想法是合并排列。T4交换的个数是具有传递性的,所以可以找连通块的信息。又因为具有单调性,可以用二分去找。然后多重集排列即可,公式\(\frac{n!}{\prods_i!}\)。T5首先,对\(a\)和\(b\)都分别排序,求出\(r_i......
  • 2023.6.4(Sun.) 练习赛总结
    题目T1打表加贪心,注意模数和一些边界情况。T4数据结构或者dp,可以从颜色角度分别计算共献,也可以从合并的角度统一计算贡献。T2首先要发现一个重要的性质:差分数组单调不降。由于差分数组可以是正的或者负的,符合要求的序列分布情况应该类似与向上开口的抛物线(∪),其中最小值在中......
  • 2023.6.8(THUR.) 练习赛总结
    链接。T2绝对值最小值,可以把原式化为两个只有一个绝对值的式子,set维护即可。T4dp用记忆化搜索加unordered_map实现的,要经过一些处理保证均摊单次转移时间复杂度是\(O(1)\)的。平时要注意计算时间复杂度要从最大的方面考虑,dp时间复杂度是状态数量乘单次转移时间,考虑一......
  • 牛客周赛 Round 29(小白)
    A.小红大战小紫#include<bits/stdc++.h>#defineIOios::sync_with_stdio(false);cin.tie(0);cout.tie(0);usingnamespacestd;intmain(){IO;inta,b;cin>>a>>b;if(a>b)cout<<"kou\n";else......
  • 【补题记录】ICPC2023 Jinan
    【补题记录】ICPC2023JinanContestLink:https://qoj.ac/contest/1472.Problems:https://sua.ac/wiki/2023-icpc-jinan/contest-zh.pdf.Solution:https://qoj.ac/download.php?type=attachments&id=1472&r=1.A.ManyManyHeadsconstintN=1e5+10;intT;str......
  • 牛客小白月赛86-水平考试
    链接:https://ac.nowcoder.com/acm/contest/73450/B来源:牛客网/*include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;stringa,b;for(inti=0;i<n;i++){cin>>a>>b;if(a.size()>b.size()){cout<<0<<endl;......
  • 牛客小白月赛86
    A一共有三盒饼干,我们先找出最多的,将最多的与其他两盒加起来的数量进行比较,如果说比最多的多我们就把这两盒给第一名,如果那两盒加起来没有最多的多,那么就把最多的给第一名B这道题我一开始的想法就是每个人不能重复那我就让小朋友排好队我一个个,第一个给1个第二个比2个以此......
  • 牛客小白月赛86(真小白)
    A.水盐平衡#include<bits/stdc++.h>#defineIOios::sync_with_stdio(false);cin.tie(0);cout.tie(0);usingnamespacestd;voidsolve(){inta,b,c,d;cin>>a>>b>>c>>d;if(a*d<c*b)cout<<&q......