随笔 - 228  文章 - 0 评论 - 7 阅读 - 55352
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

 

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

 

 

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 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 #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 }
View Code
复制代码

 

posted on   ERKE  阅读(410)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
阅读排行:
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· .NET 9 new features-C#13新的锁类型和语义
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 《SpringBoot》EasyExcel实现百万数据的导入导出
点击右上角即可分享
微信分享提示