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 ...
随机推荐
- 国内5家云服务厂商 HTTPS 安全性测试横向对比
随着 Chrome.Firefox 等浏览器对 HTTPS 的重视,国内众多云服务厂商都相继提供 SSL 证书申购服务,但是大家有没有注意到一个细节,不同厂家申请的 SSL 证书,由于证书性能.功能差 ...
- CyclicBarrier和CountDownLatch介绍
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp70 CyclicBarrier介绍 (一) 一个同步辅助类,它允许一组线 ...
- mysql添加外键约束变为索引
今天有位自己填上一坑:mysql储存引擎 原因就是数据库表引擎为:MyISAM,建立主外键关系需要是InnoDB: 解决方案:alter table table_name1 engine=inno ...
- Spring 对缓存的抽象
Cache vs. Buffer A buffer is used traditionally as an intermediate temporary store for data between ...
- 如何在centos7上安装源码包
在我们使用linux的过程中,有很多程序是通过红帽官网给的系统中安装的,但是一般来说,系统更新的速度比较慢,如果这个时候我们又想用最新版的该怎么办呢?总不能一直等系统升级吧╮(╯﹏╰)╭所以,我们可以 ...
- 软工+C(2017第7期) 野生程序员
// 上一篇:最近发展区/脚手架 // 下一篇:提问和回复 怎样做足够好的软件?我们就差一个程序员! 没有什么软件工程的理论的时候,程序员们凭借自己对编程的热爱,凭借着:"这是一个可以自动化 ...
- 软件工程(GZSD2015)第二次作业小结
第二次作业,从4月7号开始,陆续开始提交作业.根据同学们提交的作业报告,相比第一次作业,已经有了巨大改变,大家开始有了完整的实践,对那些抽象的名词也开始有了直观的感受,这很好.然后有一些普遍存在的问题 ...
- 团队作业8——第二次项目冲刺(Beta阶段)
Deadline: 2017-5-28 22:00PM,以博客发表日期为准 评分基准: 按时交 - 有分,检查的项目包括后文的三个个方面 冲刺计划安排(单独1篇博客) 七天的敏捷冲刺(每天发布1篇,共 ...
- SNS团队第六次站立会议(2017.04.27)
一.当天站立式会议照片 本次会议主要内容:汇报工作进度,根据完成情况调整进度 二.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 导入相关词库数据 研究如何存取语音.图片文件 龚晓 ...
- Java学习2——HelloWorld(编写第一个java程序)
编写 在自己的工作文件目录下(如上一篇中配置的classpath路径)创建HelloWorld.java文件,编写如下代码,并保存 public class HelloWorld { public s ...