您的位置:首页 > 其它

【高效算法设计——跳跃枚举】Uva 11093 Just Finish it up

2015-03-12 14:17 501 查看
Just Finish it up

Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu
Submit Status

Description





I I U P C 2 0 06
Problem J: Just Finish it up
Input: standard input
Output: standard output
Along a circular track, there are N gas stations, which are numbered clockwise from 1 up to N. At station i, there are p­i gallons
of petrol available. To race from station i to its clockwise neighbor one need qi gallons of petrol. Consider a race where a car will start the race with an empty fuel tank. Your task is to find whether the car
can complete the race from any of the stations or not. If it can then mention the smallest possible station i from which the lap can be completed.



Input

First line of the input contains one integer T the number of test cases. Each test case will start with a line containing one integer N, which denotes the number of gas stations. In the next
few lines contain 2*N integers. First N integers denote the values of p­is (petrol available at station i), subsequent N integers denote the value of q­is
(amount of patrol needed to go to the next station in the clockwise direction).



Output

For each test case, output the case number in the format “Case c: ”, where c is the case number starting form 1. Then display whether it is possible to complete a lap by a car with an empty
tank or not. If it is not possible to complete the lap then display “Not possible”. If possible, then display “Possible from station X”, where X is the first possible station from which the car can complete
the lap.



Constraints

- T < 25

- N < 100001

Sample Input

Output for Sample Input

2

5

1 1 1 1 1

1 1 2 1 1

7

1 1 1 10 1 1 1

2 2 2 2 2 2 2

Case 1: Not possible

Case 2: Possible from station 4

题意:给定n个加油站,每个加油站可以加油的数量以及该加油站到下一个加油站需要消耗的油量,问是否存在解使得从某个加油站为起点,走过所有加油站后回到自己,如果有多组解,输出最小的

思路:我们先从小到大枚举起点,然后依次判断能否到达下一个加油站,如果到某一点j,发现从j-1个加油站无法到达第j个加油站,那么我们跳跃枚举,直接将第j个加油站作为新的起点,因为i到j-1的加油站作为起点都不可能再到达j点了,所以这里我们只需要O(n)的时间就可以得出正解,代码如下

/*
	Author : _L
*/
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#include<bitset>
#include<functional>
#include<utility>

using namespace std;

typedef long long ll;
inline bool get(int &t)
{
    bool flag = 0 ;
    char c;
    while(!isdigit(c = getchar())&&c!='-') if( c == -1 ) break ;
    if( c == -1 ) return 0 ;
    if(c=='-') flag = 1 , t = 0 ;
    else t = c ^ 48;
    while(isdigit(c = getchar()))    t = (t << 1) + (t << 3) + (c ^ 48) ;
    if(flag) t = -t ;
    return 1 ;
}
const int maxn=100000+50;
int A[maxn];
int P[maxn];
int m,n;
bool flag;

int main()
{
    int i,j,k,tt,ans,cnt,a,b,len;
    int T;
    scanf("%d",&T);
    ll sum;
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%d",&n);

        for(i=0;i<n;i++)
        {
            scanf("%d",&A[i]);
        }
        for(i=0;i<n;i++)
        {
            scanf("%d",&P[i]);
        }
        bool isok=false;
        printf("Case %d: ",kase);
        for(i=0;i<n;)
        {
            sum=A[i];
            flag=true;
            for(j=1;j<=n;j++)
            {
               if(sum>=P[(i+j-1)%n])
               {
                   sum-=P[(i+j-1)%n];
                   sum+=A[(i+j)%n];
               }
               else
               {
                   i=i+j;
                   flag=false;
                   break;
               }

            }
            if(flag)
            {
                printf("Possible from station %d\n",i+1);
                isok=true;
                break;
            }

        }

        if(!isok)
            puts("Not possible");

    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: