"UVa 11093" Just Finish it Up (algorithmic efficiency + greed)

Source: Internet
Author: User

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&AMP;&AMP;!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&LT;=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)

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.