为什么20 样例过了 揪出必关

P2367 语文成绩

chty @ 2024-08-02 13:42:13

#include<bits/stdc++.h>
using namespace std;
int ans=99999999;
int arr[10000005];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>arr[i];
    }
    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        arr[a]+=c;
        arr[b+1]-=c;
    }
    for(int i=1;i<=n;i++){
        arr[i]+=arr[i-1];
        ans=min(arr[i],ans);
    }
    cout<<ans;
}

用的差分


by yuanzichen1 @ 2024-08-02 13:42:59

me too!!!


by DANXIBAO_Hhh @ 2024-08-02 13:44:48

输入的是成绩,而不是差


by DANXIBAO_Hhh @ 2024-08-02 13:45:10


by DANXIBAO_Hhh @ 2024-08-02 13:45:58

所以要用一个数组存差


by yuanzichen1 @ 2024-08-02 13:51:22

@DANXIBAO_Hhh 不行


by Ethereal_KQ @ 2024-09-01 12:54:34

@chty 哥,差分不是这么用的,第9行应该加一个查分数组


by Ethereal_KQ @ 2024-09-01 12:59:11

#include<bits/stdc++.h>
using namespace std;
#define N 5555555
int n,p,x,y,z,k[N],diff[N],a[N];
signed main() {
    cin>>n>>p;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        diff[i]=a[i]-a[i-1];
    }
    while(p--){
        cin>>x>>y>>z;
        diff[x]+=z;
        diff[y+1]-=z;
    }
    int minn=100;
    for(int i=1;i<=n;i++){
        k[i]=k[i-1]+diff[i];
        minn=min(minn,k[i]);
    }
    cout<<minn;
    return 0;
}

|