60求助

P2367 语文成绩

Visualcode @ 2024-08-04 23:54:31

使用差分区间加法+sort排序

#include<cstdio>
#include<algorithm>
using namespace std;
int a[10005],b[10005],n,m,l,r,c;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i]-a[i-1];
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&l,&r,&c);
        b[l]+=c;
        b[r+1]-=c;
    }
    for(int i=1;i<=n;i++){
        a[i]=a[i-1]+b[i];
    }
    sort(a+1,a+1+n);
    printf("%d",a[1]);
    return 0;
}

by lihaoyu114514 @ 2024-08-05 00:34:14

数组开到5e6+10就够了,


by lihaoyu114514 @ 2024-08-05 00:35:36

还有,n log n的sort排序 n=5e6时排序铁定TLE啊


by lihaoyu114514 @ 2024-08-05 00:37:25

你可以在第三个for循环中还原后用ans记录最小值,最后直接输出ans就可以了


by lihaoyu114514 @ 2024-08-05 00:38:11

你可以看看我的代码:


using namespace std;
const int N=5e6+10;
long long a[N];
long long s[N];
long long n,x,l,r,c,minx=300;
void fn(int l,int r,int c) 
{
    s[l]+=c;
    s[r+1]-=c;
}
int main()
{
    cin>>n>>x;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        fn(i,i,a[i]);
    }
    while(x--)
    {
        cin>>l>>r>>c;
        fn(l,r,c);
    }
    for(int i=1;i<=n;i++)
    {
        s[i]=s[i]+s[i-1];
        minx=min(s[i],minx);
    }
    cout<<minx;
    return 0;
}

by lihaoyu114514 @ 2024-08-05 00:47:19

给你改好了:


#include<algorithm>
#include<cmath>
using namespace std;
int a[5000005],b[5000005],n,m,l,r,c;
int main(){
    int ans=1e9;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i]-a[i-1];
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&l,&r,&c);
        b[l]+=c;
        b[r+1]-=c;
    }
    for(int i=1;i<=n;i++){
        a[i]=a[i-1]+b[i];
        ans=min(ans,a[i]);
    }
    printf("%d",ans);
    return 0;
}

by Visualcode @ 2024-08-05 01:09:49

@lihaoyu114514

已关注

感谢大佬


|