zoukankan      html  css  js  c++  java
  • hnust Fuel Stops

    Fuel Stops
    Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:32768KB
    Total submit users: 25, Accepted users: 21
    Problem 11344 : No special judgement
    Problem description
      

          You are required to take a circular tour of a given set of cities: start at a certain city, visit each city once, and return to the city at which you started.    

          Each city is identified by a number: 1, 2, 3, etc. The numbering of the cities specifies the path you must take, but the starting point is not specified. From the highest numbered city, you proceed to City 1. For example, if there are three cities (numbered 1, 2, 3) you have three choices for completing the tour:    

          Start at 1, proceed to 2, then to 3, then return to 1.       Start at 2, proceed to 3, then to 1, then return to 2.       Start at 3, proceed to 1, then to 2, then return to 3.    

          There is a refueling station in each city, with a given quantity of available fuel. The sum of all the fuel supplies at the refuelling stations is equal to the fuel required to make the entire tour. You start with an empty tank at one of the refuelling stations. You will be running out of fuel just as you pull into the starting station upon successful completion of the tour.    

          You must determine which city (or cities) will qualify as a starting point for the tour without running out of fuel before returning to the starting station. Assume the fuel tank is sufficiently large to handle all of the refuelling operations.    

    Input
      

          The input will contain data for several test cases. For each test case, there will be three lines of data. The first line will specify the number of cities as a single integer. The second line will specify the quantity of fuel available at each of the refuelling stations, in the order of city numbers: 1, 2, 3, etc. The third line will specify the quantity of fuel needed to get from each station to the next one, in the order of city numbers: from the station at city 1 to the station at city 2, from the station at city 2 to the station at city 3, etc; the last number specifies the quantity of fuel required to get from the highest numbered city's station back to the station at city 1. All fuel quantities are positive integers given in imperial gallons. The sum of the fuel supplies will not exceed the range of signed 32-bit integers. There will be at least two cities and up to 100000 cities. End of input will be indicated by a line containing zero for the number of cities; this line will not be processed.    

    Output
      

          For each test case, there will be one line of output. After the case number, the output will list the city numbers that work as starting cities for a successful tour, as described above. In case of several possible starting cities, they must be listed in increasing order separated by a single space. Follow the format of the sample output. The Hungarian mathematician L. Lovász proved that there is always at least one possible starting city.    

    Sample Input
    3
    3 2 2
    4 2 1
    3
    3 2 1
    1 3 2
    4
    3 4 5 2
    2 3 8 1
    0
    Sample Output
    Case 1: 2 3
    Case 2: 1
    Case 3: 4
    Problem Source
      2012 Rocky Mountain Regional Contest

       思路:题意比较简单。O(N)的算法,尺矩法吧(这个),就是枚举起点,虽然不是什么很牛叉的算法,但是一般2个while的代码都是容易犯错的。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <map>
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #define Max(a,b) ((a)>(b)?(a):(b))
    using namespace std;
    typedef long long LL ;
    const int size=100008  ;
    int k=1 ;
    struct Me{
        int N ;
        int come[size] ;
        int lost[size] ;
        Me(){} ;
        Me(int n):N(n){
            for(int i=1;i<=N;i++)
                scanf("%d",&come[i]) ;
            for(int i=1;i<=N;i++)
                scanf("%d",&lost[i]) ;
        }
        void  gao(){
            printf("Case %d:",k++) ;
            int have ;
            int go ;
            int head ,rear ;
            int city=1 ;
            have=go=0 ;
            head=rear=1 ;
            while(head<=N){
                have+=come[rear] ;
                go+=lost[rear] ;
                if(have<go){
                    while(have<go){
                        have-=come[head] ;
                        go-=lost[head] ;
                        head++ ;
                        city-- ;
                    }
                }
                rear++ ;
                city++ ;
                if(rear==N+1)
                  rear=1 ;
                if(city==N){
                   printf(" %d",head) ;
                   have-=come[head] ;
                   go-=lost[head] ;
                   head++ ;
                   city-- ;
                }
            }
            puts("") ;
        }
    };
    int main(){
       int n , m ;
       while(scanf("%d",&n)&&n){
           Me me(n) ;
           me.gao() ;
       }
       return 0 ;
    }
    
  • 相关阅读:
    LR网页细分图中的时间详解
    LoadRunner系列实例之— 01录制cas登陆脚本
    Oracle 身份证校验
    Oracle中执行存储过程call和exec区别
    MD5 加密的密码在数据库重置
    python学习 (二十九) range函数
    python学习 (二十八) Python的for 循环
    二十一 守护线程
    二十 线程的优先级
    十九 yield方法
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3299740.html
Copyright © 2011-2022 走看看