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 |
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 int a[220000],n; 6 int work() 7 { 8 int sum=0,l=0,r=0,i,m=n<<1; 9 for(i=n; i<m; i++)a[i]=a[i-n]; 10 while(1) 11 { 12 while(l<n&&sum<0)sum-=a[l++]; 13 if(l==n)break; 14 while(r<m&&sum>=0) 15 { 16 sum+=a[r++]; 17 if(r-l==n&&sum>=0)break; 18 } 19 if(r-l==n&&sum>=0)break; 20 } 21 return l+1; 22 } 23 int main() 24 { 25 int t,r,i,suma,sumb,x; 26 scanf("%d",&t); 27 for(r=1; r<=t; r++) 28 { 29 scanf("%d",&n); 30 suma=sumb=0; 31 for(i=0; i<n; i++)scanf("%d",&a[i]),suma+=a[i]; 32 for(i=0; i<n; i++)scanf("%d",&x),sumb+=x,a[i]-=x; 33 printf("Case %d: ",r); 34 if(suma<sumb) 35 { 36 printf("Not possible\n"); 37 } 38 else 39 printf("Possible from station %d\n",work()); 40 } 41 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· .NET 9 new features-C#13新的锁类型和语义
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 《SpringBoot》EasyExcel实现百万数据的导入导出