Just Finish it up UVA - 11093
| 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());
}
}
Just Finish it up UVA - 11093的更多相关文章
- UVa 11093 Just Finish it up
从第一个加油站开始枚举起点,如果到第i个加油站油量不够的话,那么1~i个加油站都不可能是起点. 将第i+1个加油站作为起点继续枚举. 比如说,第一个加油站开始最多跑到第5个加油站,那么第二个加油站不可 ...
- UVA 11093 Just Finish it up 环形跑道 (贪心)
有一个环形跑道,上面有n个加油站,到i号加油站可以加pi的油,跑到下一站要花费qi的油,起点任意选,问是否有一个起点可跑完整个跑道. 从i开始跑,如果遇到某个站j不能跑了,那么从i到j之间的站开始跑, ...
- 【例题 8-13 UVA - 11093】Just Finish it up
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法. 假设现在取[l..r]这一段. 然后发现累加的和小于0了. 那么方法只能是不走l..l+1这一段了 即delta递减(p[ ...
- UVA - 11093 Just Finish it up(环形跑道)(模拟)
题意:环形跑道上有n(n <= 100000)个加油站,编号为1~n.第i个加油站可以加油pi加仑.从加油站i开到下一站需要qi加仑汽油.你可以选择一个加油站作为起点,起始油箱为空(但可以立即加 ...
- 【uva 11093】Just Finish it up(算法效率--贪心)
题意:环形跑道上有N个加油站,编号为1~N.第 i 个加油站可以加油Ai加仑,从加油站 i 开到下一站需要Bi加仑汽油.问可作为起点走完一圈后回到起点的最小加油站编号. 解法:我们把每个加油站的Ai, ...
- UVa 11093 环形跑道(模拟)
https://vjudge.net/problem/UVA-11093 题意:环形跑道上有n个加油站,编号为1~n.第i个加油站可以加油pi加仑,从加油站i开到下一站需要qi加仑汽油.输出最小的起点 ...
- 紫书 例题8-13 UVa 11093 (反证法)
这道题发现一个性质就解决了 如果以i为起点, 然后一直加油耗油, 到p这个地方要去p+1的时候没油了, 那么i, i+1, --一直到p, 如果以这些点 为起点, 肯定也走不完. 为什么呢? 用反证法 ...
- UVa 11729 - Commando War(贪心)
"Waiting for orders we held in the wood, word from the front never came By evening the sound of ...
- Uva 11729 Commando War (简单贪心)
Uva 11729 Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...
随机推荐
- C# 反射、与dynamic最佳组合
在 C# 中反射技术应用广泛,至于什么是反射.........你如果不了解的话,请看下段说明,否则请跳过下段.广告一下:希望我文章的朋友请关注一下我的blog,这也有助于提高本人写作的动力. 反射:当 ...
- C++ 用lambda代替 unique_ptr 的Deleter
C++ 用lambda代替 unique_ptr 的Deleter 代码 #include <iostream> #include <cstdlib> #include < ...
- Java 反射之动态代理
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt205 利用Java反射机制你可以在运行期动态的创建接口的实现.java.la ...
- Liunx-常用命令的总结(5)
cd ../dir 上一节目录下dir目录 cd - 返回上次目录 ifconfig 查看IP地址 sudo if ...
- poj3463 最短路和比最短路长1的路径数
这题需要很好的理解Dij. 在Dij的基础上,每个点多一个次短路的长度和数量进行控制. 那么在队列中,最短路控制时出现n次,次短路控制出现n次.注意松弛条件中val值和最短路.次短路的关系. 这题需要 ...
- Java线程调度—休眠
线程休眠的方法是Thread.sleep(long millis) 和Thread.sleep(long millis, int nanos) ,均为静态方法,那调用sleep休眠的哪个线程呢?简单说 ...
- 跨浏览器tab页的通信解决方案尝试
目标 当前页面需要与当前浏览器已打开的的某个tab页通信,完成某些交互.其中,与当前页面待通信的tab页可以是与当前页面同域(相同的协议.域名和端口),也可以是跨域的. 要实现这个特殊的功能,单单使用 ...
- 如何将RDS的数据同步到本地自建数据库
长期以来有很多的用户咨询如何将RDS的数据同步到本地的数据库环境中,本篇文章以在阿里云的ECS服务器为例来说明如何将RDS的数据同步到本地数据库中.RDS对外提供服务是一个DNS地址+端口3306,这 ...
- 【集美大学1411_助教博客】团队作业1——团队展示&选题 成绩
第一次团队作业已经新鲜出炉啦,各位同学请查收.截止日期前,全班都按时提交了作业,而且有的团队还提交了两次呢,下次不要这样啦~ 题目 团队作业1--团队展示&选题 回顾 个人作业1--四则运算题 ...
- 201521123020《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 运用到了equ ...