ACM/ICPC 2018 Asia qualifiers Beijing Race Station Online tournament

Source: Internet
Author: User

Test instructions: To a city to get money, to leave to spend money. There is cash at the beginning. The city is circular, ask from which start, can on the way any time money >=0;

A start pointer I, a trailing pointer J. Indicates an interval. If the conditional ++j is met and the proceeds are added to the sum (the earnings may be negative). Non-conforming is ++i, and the return from sum proceeds until the sum>=0; interval length is n, the position of I is the result.

Proof of correctness: Suppose [A, b] is the first legal interval. At some point i,j are <a,i to the left of a, so J cannot extend to the right of B (otherwise [A, b] is not the first legal interval). It is only possible that J still falls to the left of a, or to the middle of a B. When I<a, J cannot extend to the right of B. When J is in the middle of a, I cannot be retreated to the right.

Such a ruler is used in the past to find and greater than a certain value of the shortest continuous interval.

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<iomanip>#include<cstring>#include<map>#include<queue>#include<Set>#include<cassert>#include<stack>using namespacestd;Const Doubleeps=1e-8; typedefLong LongLon;ConstLon sz=1000050, inf=0x7FFFFFFF; Lon arr[4*Sz],nex[sz];intMain () {Std::ios::sync_with_stdio (0); //freopen ("D:\\1.txt", "R", stdin);Lon Casenum; CIN>>Casenum; //scanf ("%d", &casenum);     for(Lon Time=1; time<=casenum;++Time )//for (Lon time=1;scanf ("%d%d", &n,&k)!=eof;++time){lon n,c; CIN>>n>>C;  for(Lon I=1; i<=n;++i) cin>>Arr[i];  for(Lon I=1; i<=n;++i) {cin>>Nex[i]; Arr[i]-=Nex[i]; }         for(inti=n+1; i<=2*n;++i) arr[i]=arr[i-N]; intsum=0, cnt=0, bg=1; BOOLok=0;  for(intI=1; i<=2*n;++i) {if(sum+arr[i]+c<0)            {                 for(;bg<=i&&sum+arr[i]+c<0;++BG) {                    if(bg<i) sum-=ARR[BG]; }                if(bg<=i) sum+=Arr[i]; }            Else            {                //if (bg==0) bg=i;sum+=Arr[i]; if(i-bg+1>=N) {OK=1;  Break; }            }        }//cout<< "" <<ok<<endl;        if(OK) cout<<bg<<Endl; Elsecout<<-1<<Endl; }    return 0;}

ACM/ICPC 2018 Asia qualifiers Beijing Race Station Online tournament

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.