51nod 1065 min Positive sub-segment and (greedy)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.