title: Portal.
Test Instructions: Chinese question.
The main puzzle : prefix and , and mark each number of subscript, according to the prefix and size of small to large order. Subsequent traversal, if the subscript data[i-1].id<data[i].id&&data[i-1].val<data[i].valis satisfied, the minimum value is updated, because it is similar, So it's a minimum candidate, and the rest is absolutely impossible. So why only consider neighboring? Because if A to B does not form a queue, a to C forms a queue, then B to C must be smaller than a to C, and must be able to form a queue (A and B cannot form a queue, indicating POSA>POSB, A and C can form a queue, indicating posa< PosC, there must be Posb<posc). It is important to think of the prefix and the tree-like array in the direction of O (N) when encountering such problems.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd;structsa{Long LongVal//to use a long long intID;} data[50005];BOOLCMP (sa x, sa y) {returnx.val<y.val;//Sort Ascending}intMain () {intN; while(cin>>N) {data[0].val=0; data[0].id=0; for(intI=1; i<=n;i++) {cin>>Data[i].val; Data[i].val+=data[i-1].val; Data[i].id=i; } sort (Data,data+n+1, CMP);//Note that this contains the first item 0 BOOLfirst=true; Long Longans=0; for(intI=1; i<=n;i++) { if(data[i].id>data[i-1].id&&data[i].val>data[i-1].val) {if(first) {ans=data[i].val-data[i-1].val; First=false; } Elseans=min (ans, (data[i].val-data[i-1].val));//take min not max}} cout<<ans<<Endl; } return 0;}
51nod 1065 min Positive sub-segment and (greedy)