Links: https://ac.nowcoder.com/acm/contest/949/D
Source: Cattle-off network
Title Description
Fruit store has
the n- the n-fruit in a row. The manager can only ask the customer to buy a contiguous fruit.
Xiaoyang for each fruit has a likeability A i ai, the final degree of satisfaction as he likes to buy fruit and.
And if positive (no matter how positive, as long as greater than 0 0 can be), he'll be happy.
Xiaoyang for each fruit has a likeability A i ai, the final degree of satisfaction as he likes to buy fruit and.
And if positive (no matter how positive, as long as greater than 0 0 can be), he'll be happy.
Xiaoyang want to know to buy the maximum number of fruit under the conditions of his satisfaction.
Can you help him?Enter a description:
The first input line of a positive integer n, the total number of fruits.
The second line of the input n integers A I AI, represents a small extent like for each of the male fruit.
Output Description:
A row of integer results. (If one can not buy fruit, please output 0)
Example 1
Entry
5 0 0 -7 -6 1
Export
1
ideas: for each prefix and saved and the position of a structure. And descending order according to the prefix, the prefix, and according to the position equal descending rows. Traversing again. Records have traversed
a minimum position minx, if the current position of the point of traversing a [i] .pos greater than the minimum front position, can update answer (a [i] .pos-minx ), and since the prefix is larger than it, It must be a
positive number.
However, to note is that we must add a zero, and the prefix 0, 0 position, if the test sample is only a 1-point, there is no point of comparison, so to add a zero.
In fact, the prefix and equal by location descending row is very important because guarantees a minimum position in front of the prefix and must be smaller than it. Prefix and will not appear equal, resulting in an updated
answer wrong.
Code:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int maxn=2e6+100; struct node{ int num,pos; }e[maxn]; 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,x; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&x); e[i].num=x+e[i-1].num;//前缀和 e[i].pos=i;//位置 } n++; sort(e+1,e+n+1,cmp); int minx=n; int ans=0; for(int i=1;i<=n;i++) { minx=min(minx,e[i].pos); if(e[i].pos>minx) ans=max(ans,e[i].pos-minx); } printf("%d\n",ans); return 0; }