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

本文探讨了一个经典的算法问题:在环形赛道上,给定各加油站的油量和行驶至下一加油站所需的油量,判断是否能从某个起点完成一圈,并找出最优起点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Submit Status

Description

Download as PDF

 

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 i gallons of petrol available. To race from station 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 is (petrol available at station i), subsequent N integers denote the value of 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值