uva11093 just finish it up

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 q i gallons of petrol. Consider a race where a car will start the
race with an empty fuel tank. Your task is to nd 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 i s (petrol available at station i ), subsequent N integers
denote the value of q i s (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
rst possible station from which the car can complete the lap.
Constraints • T < 25 • N < 100001

考虑i号点,如果以它为起点不行,其中是在j点到j+1点处失败的,那么i+1..j中任何一个做起点都不行。因为到达该点时,油箱里的油大于等于零。如果这都不行,那么作为起点以空油箱开始就更不行了。
于是可以O(n)解决。

#include<cstdio>
#include<cstring>
#define M(a) memset(s,0,sizeof(a))
int p[100010],q[100010];
int main()
{
    int i,j,k,m,n,x,y,z,tot,T;
    bool b,bb,bbb;
    scanf("%d",&T);
    for (k=1;k<=T;k++)
    {
        scanf("%d",&n);
        for (i=1;i<=n;i++)
          scanf("%d",&p[i]);
        for (i=1;i<=n;i++)
          scanf("%d",&q[i]);
        i=1;
        b=0;
        while (i<=n)
        {
            j=i;
            tot=0;
            bb=1;
            bbb=1;
            while (bbb||j!=i)
            {
                bbb=0;
                tot+=p[j]-q[j];
                if (tot<0)
                {
                    bb=0;
                    break;
                }
                j++;
                if (j==n+1) j=1;
            }
            if (bb)
            {
                b=1;
                break;
            }
            if (j<i) break;
            i=j+1;
        }
        if (!b) printf("Case %d: Not possible\n",k);
        else printf("Case %d: Possible from station %d\n",k,i);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值