Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
I I U P C 2 0 0 6 |
|
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 pi 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 pis (petrol available at station i), subsequent N integers denote the value of qis (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 |
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[],n;
int work()
{
int sum=,l=,r=,i,m=n<<;
for(i=n; i<m; i++)a[i]=a[i-n];
while()
{
while(l<n&&sum<)sum-=a[l++];
if(l==n)break;
while(r<m&&sum>=)
{
sum+=a[r++];
if(r-l==n&&sum>=)break;
}
if(r-l==n&&sum>=)break;
}
return l+;
}
int main()
{
int t,r,i,suma,sumb,x;
scanf("%d",&t);
for(r=; r<=t; r++)
{
scanf("%d",&n);
suma=sumb=;
for(i=; i<n; i++)scanf("%d",&a[i]),suma+=a[i];
for(i=; i<n; i++)scanf("%d",&x),sumb+=x,a[i]-=x;
printf("Case %d: ",r);
if(suma<sumb)
{
printf("Not possible\n");
}
else
printf("Possible from station %d\n",work());
}
}