求助,不知道为什么会WA第二和第五个点

P2367 语文成绩

Green_Yeast_King @ 2022-07-08 22:05:27

RT


#include <bits/stdc++.h>
using namespace std;
const int N = 5e7+1;
int n, p, x, y, z, a[N], minx;
int main() {
    ios :: sync_with_stdio (false);
    cin >> n >> p;
    for (int i = 1; i <= n; i ++ )
        cin >> a[i], a[i] -= a[i - 1];
    for (int i = 1; i <= p; i ++ ) {
        cin >> x >> y >> z;
        a[x] += z;
        a[y + 1] -= z;
    }
    minx = a[1];
    for (int i = 2; i <= n; i ++ ) {
        a[i] += a[i - 1];
        minx = min (minx, a[i]);
    }
    cout << minx << endl;
    return 0;
}

by anonymous_letter @ 2022-07-08 22:11:23

for (int i = 1; i <= n; i ++ )
        cin >> a[i], a[i] -= a[i - 1];

中的

a[i] -= a[i - 1];

应该是要倒着减的吧,而且

for (int i = 2; i <= n; i ++ ) {
        a[i] += a[i - 1];
        minx = min (minx, a[i]);
    }

应该从1开始循环


by Green_Yeast_King @ 2022-07-08 22:16:27

@fictitious_love AC了,谢谢大佬


by dream_Hao @ 2022-07-21 11:18:32

@fictitious_love 大佬,倒着减是啥意思啊


by lyc1001 @ 2022-08-01 16:22:50

@dream_Hao for(i=n;i>=1;i--),或者另开一个差分数组。否则,举个例子:输入3,数组为3,输入4,数组为3,4-3.输入5,应该是3,4-3,5-4,但实际上,3,4-3,5-4+3,。前缀和和差分这块要不想要莫名其妙的错误,还是两个数组为好


|