题目链接:https://ac.nowcoder.com/acm/contest/949/D
AC代码:
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int num;
int pos;
};
Node node[2000005];
int a[2000005];
bool cmp(Node a, Node b)
{
if(a.num == b.num)
return a.pos > b.pos;
return a.num < b.num;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
node[i].num = node[i-1].num + a[i];
node[i].pos = i;
}
n++;//加一项(0,0)
sort(node+1, node+1+n, cmp);
int minn = n;
int ans = 0;
for(int i = 1; i <= n; i++)
{
minn = min(minn, node[i].pos);
if(minn < node[i].pos)
ans = max(ans, node[i].pos - minn);
}
printf("%d\n", ans);
return 0;
}