比赛 4043级2023省选模拟赛7 评测结果 AAAAAAAAAAAAAA
题目名称 Equal Sum Subarrays 最终得分 100
用户昵称 op_组撒头屯 运行时间 9.960 s
代码语言 C++ 内存使用 8.95 MiB
提交时间 2023-03-29 08:59:04
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
const int N=500+5;
const ll inf=1e18;
int n,cnt=0;
ll a[N],sum[N];
struct sdf{
    ll val;int l,r;
}p[N*N];
bool cmp(sdf x,sdf y){
    return x.val<y.val;
}
bool ins(int x,int y){
    return p[x].l<=y&&y<=p[x].r;
}
int main(){
	freopen ("dhzsz.in","r",stdin);
	freopen ("dhzsz.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
	for (int i=1;i<=n;i++){
	    for (int j=i;j<=n;j++)p[++cnt]={sum[j]-sum[i-1],i,j};
    }
    sort(p+1,p+cnt+1,cmp);
    for (int i=1;i<=n;i++){
        ll ans=inf,lst=-inf;
        for (int j=1;j<=cnt;j++){
            if (ins(j,i))ans=min(ans,p[j].val-lst);
            else lst=p[j].val;
        }
        lst=inf;
        for (int j=cnt;j>=1;j--){
            if (ins(j,i))ans=min(ans,lst-p[j].val);
            else lst=p[j].val;
        }
        printf("%lld\n",ans);
    }
}