WA求助

P2367 语文成绩

The_Wandering_Earth @ 2022-12-29 18:46:42

#include<bits/stdc++.h> 
using namespace std;
int s[5000001],a[5000001],n,p,x,y,z,sum,ans=2e9;
signed main(){
    cin>>n>>p;
    for(int i=1;i<=n;i++)cin>>a[i],s[i]=a[i]-a[i-1];
    for(int i=1;i<=p;i++){
        cin>>x>>y>>z;
        a[x]+=z,a[y+1]-=z;
    }
    for(int i=1;i<=n;i++)sum=a[i-1]+s[i],ans=min(ans,sum);
    cout<<ans;
}

0分全wa,大佬们帮忙看看哪里错了吧,悬赏一个关注


by liangbowen @ 2022-12-29 18:48:21

@zhangpeinan123 建议了解一下差分数组的用法。改成这样就过了。

//帮人调题
#include<bits/stdc++.h> 
using namespace std;
int s[5000001],a[5000001],n,p,x,y,z,sum,ans=2e9;
signed main(){
    cin>>n>>p;
    for(int i=1;i<=n;i++)cin>>a[i],s[i]=a[i]-a[i-1];
    for(int i=1;i<=p;i++){
        cin>>x>>y>>z;
        s[x]+=z,s[y+1]-=z; //更改
    }
    for(int i=1;i<=n;i++)sum+=s[i],ans=min(ans,sum); //更改
    cout<<ans;
}

by The_Wandering_Earth @ 2022-12-29 18:50:11

@liangbowen 感谢,主要是不长眼,把赋值那里写错了,已关注


|