差分写法60分WA (麻烦各位神犇帮我看看)

P2367 语文成绩

_weishiqi66_ @ 2022-12-29 17:26:51

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 6 * int(1e6);
int n, p, ans = 1e9;
int d[N], a[N];

int main() {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) {
        cin >> d[i];
        d[i] = d[i] - d[i - 1];
    }
    for (int i = 1; i <= p; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        d[x] += z;
        d[y + 1] -= z;
    }
    for (int i = 1; i <= n; i++) {
        a[i] = d[i] + a[i - 1];
        ans = min(ans, a[i]);
    }

    cout << ans;
    return 0;
}

by FQR_ @ 2022-12-29 17:34:21

@weishiqi66 再建一个数组 b,然后把输入部分改成:

for (int i = 1; i <= n; i++) {
        cin >> b[i];
        d[i] = b[i] - b[i - 1];
 }

by FQR_ @ 2022-12-29 17:35:59

假设 k 是差分数组,p 是原数组,则 k_i=p_i-p_{i-1}。楼主的思路是 k_i=p_i-k_{i-1}


by _weishiqi66_ @ 2022-12-29 17:36:46

@FQR_ 哦!我居然犯了这么大一个致命错误!谢谢大佬orz


|