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