Test instructions: There are N petrol stations on the circular runway, numbered 1~n. The I petrol station can refuel AI gallons, from gas station I to the next station need bi gallons of gasoline. Ask the smallest gas station number that can be used as a starting point to go back to the starting point.
Solution: We combine the ai,bi of each gas station, and treat Ai-bi as the right CI of n points, indicating the remaining oil quantity after I. It is known that the first petrol station is sum{}+ci>=0,sum{} indicating the amount of oil remaining before opening from the starting point to I, sum{}>=0. Therefore, if sum{}+ci<0, then from the beginning of the enumeration from the starting point to I all points can not be used as a starting point, because at this time the sum{} is already with their starting point to reach the maximum value, there is no more oil residue, must not pass the point I. Enumerates i+1 as the starting point and continues to judge.
P.S. Because it is a ring, it is a bit of trouble to judge to walk a circle, we should be careful. I think I played a short, but not very beautiful. = =
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6#include <queue>7 using namespacestd;8 Const intn=100010;9 Ten intN; One intA[n],b[n]; A intMmax (intXintY) {returnX>y?x:y;} - intCheckintx) - { the intH=0, t=x; - BOOLtf=true; - while(1) - { + if(T==X&&!TF)return-1; -h+=a[t]-B[t]; + if(h<0)returnT; Atf=false; atT= (t+1)%N; - } - } - intMain () - { - intT; inscanf"%d",&T); - for(intKase=1; kase<=t;kase++) to { +scanf"%d",&n); - for(intI=0; i<n;i++) scanf ("%d",&a[i]); the for(intI=0; i<n;i++) scanf ("%d",&b[i]); * intans=n,x=0, tmp,mx=-1; $ while(1)Panax Notoginseng { - if(X<=MX) Break; theTmp=check (x), mx=Mmax (mx,x); + if(tmp==-1) {ans=x; Break;} A Elsex=tmp+1; the } + if(ans!=n) printf ("Case%d:possible from station %d\n", kase,ans+1); - Elseprintf"Case %d:not possible\n", Kase); $ } $ return 0; -}
"UVa 11093" Just Finish it Up (algorithmic efficiency + greed)