11093 - 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 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

优化:

1.在输入两个数组的时候,分别计算sump和sumq,若sump<sumq,无论起点是谁,都不可能。

2.在输入数组q的时候,只有满足p[i]>=q[i]的点才能作为起点,用一个数组来做标记。

3.假设1号站可以作为起点,在模拟的过程中,在加油站p处油没了,那么1—p之间的所有点都不可能是解。

解释:我们用lefti表示从起点到点i的总剩余量。首先p处必定不可能(因为leftp-1>0,leftp<0,所以p必定不能为起点);1到任意一个在(1,p)的点i,lefti-1>=0,若以I为起点,I到p点的总剩余量=leftp-lefti<0,所以(1,p)的任意一点都不行。

代码:

#include<iostream>

#include<cstring>

using namespacestd;

 

const intmaxn=100001+10;

intp[maxn],q[maxn],flag[maxn];

 

int main()

{

    ios::sync_with_stdio(false);

    int t;

    cin>>t;

    int kase=1;

    while(t--)

    {

        memset(flag,0,sizeof(flag));

        int n;

        cin>>n;

        int sump=0,sumq=0;

        for(int i=0; i<n; i++)

        {

            cin>>p[i];

            sump=sump+p[i];

        }

        for(int i=0; i<n; i++)

        {

            cin>>q[i];

            sumq=sumq+q[i];

            if(p[i]>=q[i])

            {

                flag[i]=1;

            }

        }

        cout<<"Case"<<kase++<<": ";

        if(sump>=sumq)

        {

            bool flag2=false;

            for(int i=0; i<n; i++)

            {

                bool flag1=true;

                if(flag[i])

                {

                    int left=p[i]-q[i];

                    for(int j=1; j<n; j++)

                    {

                       left=left+p[(i+j)%n]-q[(i+j)%n];

                        if(left<0)

                        {

                            flag1=false;

                            if((i+j)%n>i&&(i+j)%n<n-1)

/*

当前加油站为i,若走到i之前的加油站油没了((i+j)%n<i),那么无需更新(我们从0号加油站开始枚举,i之前的所有加油站已经枚举过了,i=(i+j)%n会使得i更小,);若走到最后一个加油站(编号为N-1)油没了,如果此时更新i的值,下一次循环时,i变成了0,又从头开始枚举,导致死循环。

*/

                            {

                                i=(i+j)%n;

                            }

                            break;

                        }

                    }

                    if(flag1)

                    {

                       cout<<"Possible from station "<<(i+1)<<endl;

                        flag2=true;

                        break;

                    }

                }

            }

            if(!flag2)

            {

                cout<<"Notpossible\n";

            }

        }

        else

        {

            cout<<"Notpossible\n";

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值