1 条题解

  • 0
    @ 2024-6-4 15:05:24

    仔细读懂数据生成器,就能看出来,实际上物品肯定是够用的。

    因为只能从右向左搬运物品,所以我们只需要对于每一个i,i+1i,i+1的间隔,考虑有多少个物资需要从右边搬到左边去,把这个贡献累加即可,时间复杂度O(n)O(n)

    #include <bits/stdc++.h>
    #define maxn 10000005
    #define ll long long
    using namespace std;
    ll n;
    ll c[maxn];
    int a[maxn], b[maxn];
    int randseed;
    unsigned int rnd() {
        unsigned int r;
        r = randseed = randseed * 1103515245 + 12345;
        return (r << 16) | ((r >> 16) & 0xFFFF);
    }
    
    int main() {
        cin >> n >> randseed;
        int sum = 0;
        for (int i = n; i >= 1; i--) {
            a[i] = rnd() % 1000, b[i] = rnd() % 1000;
            if (sum + (a[i] - b[i]) < 0)
                swap(a[i], b[i]);
            sum += (a[i] - b[i]);
        }
        for (int i = 1; i <= n; i++) c[i] = a[i] - b[i];
        ll ans = 0, now = 0;
        for (int i = 1; i <= n; i++) {
            ans += now;
            if (c[i] < 0)  //有需求
                now += -c[i];
            else  //有供给
                now -= min(now, c[i]);
        }
        cout << ans << endl;
    }
    
    • 1

    信息

    ID
    1417
    时间
    1000ms
    内存
    512MiB
    难度
    6
    标签
    (无)
    递交数
    87
    已通过
    24
    上传者