UVA11093-Just Finish it up(思维)
Accept: 1225 Submit: 5637
Time Limit: 3000 mSec
Problem Description
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
Constraints
• T < 25
Sample Input
Sample Output
Case 1: Not possible
Case 2: Possible from station 4
题解:比较经典的问题,从1开始尝试,如果从p道p+1这一段走不到那就说明从2到p都不能走完全程,简单解释一下,如果能从1走到2,那么从1开始不会比从2开始更差,因为到2时的油>=0,因此1走不完,2更走不完,同理到p都走不完,直接从p+1开始试就好,复杂度线性。
#include <bits/stdc++.h> using namespace std; const int maxn = + ; int n, con = ;
int sup[maxn], req[maxn]; int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &sup[i]);
}
for (int i = ; i < n; i++) {
scanf("%d", &req[i]);
} printf("Case %d: ", con++);
int st = ;
while (st < n) {
int tmp = st;
int cur = , T = n;
while (T--) {
cur += sup[tmp];
cur -= req[tmp];
if (cur < ) break;
tmp++;
tmp %= n;
}
if (T == -) break;
if (tmp + <= st) {
st = n;
break;
}
else st = tmp + ;
}
if (st == n) {
printf("Not possible\n");
}
else printf("Possible from station %d\n", st+);
}
return ;
}
UVA11093-Just Finish it up(思维)的更多相关文章
- 8-13 Just Finish it up uva11093
题意:环形跑道上有n n<=100000 个加油站 编号为1-n 第i个加油站可以加油pi加仑 从加油站i开到下一站需要qi加仑 你可以选择一个加油站作为起点 初始油箱为空 如果 ...
- Codeforces Round #426 (Div. 2)【A.枚举,B.思维,C,二分+数学】
A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Inno Setup设置在安装Finished页面,点击finish后打开网页
在安装的最后一个页面FinishPage中点击Finished然后打开一个网页 这个功能貌似很简单,不就是在点击finish按钮给它绑定事件,问题立马解决. 在普通的桌面应用程序开发中的确是这样做的, ...
- D. Eternal Victory(dfs + 思维)
D. Eternal Victory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- Photoshop、Illustrator思维导图笔记
半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.
- CYQ.Data 从入门到放弃ORM系列:开篇:自动化框架编程思维
前言: 随着CYQ.Data 开始回归免费使用之后,发现用户的情绪越来越激动,为了保持这持续的激动性,让我有了开源的念头. 同时,由于框架经过这5-6年来的不断演进,以前发的早期教程已经太落后了,包括 ...
- 计算机程序的思维逻辑 (8) - char的真正含义
看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...
- 计算机程序的思维逻辑 (29) - 剖析String
上节介绍了单个字符的封装类Character,本节介绍字符串类.字符串操作大概是计算机程序中最常见的操作了,Java中表示字符串的类是String,本节就来详细介绍String. 字符串的基本使用是比 ...
随机推荐
- spring_05装配bean
一.前言 <bean id="user1" scope="singleton" init-method="myInit" destro ...
- redis复制
一.官网解释 redis支持简单且易用的主从复制 ( master-slave replication )功能,改功能可以让服务器 ( slave server ) 成为主服务器 ( master s ...
- Java中关于Map的使用(HashMap、ConcurrentHashMap)
在日常开发中Map可能是Java集合框架中最常用的一个类了,当我们常规使用HashMap时可能会经常看到以下这种代码: Map<Integer, String> hashMap = new ...
- 【LInux】查看Linux系统版本信息
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/versionLinux version 2.6. ...
- JavaScript机器学习之KNN算法
译者按: 机器学习原来很简单啊,不妨动手试试! 原文: Machine Learning with JavaScript : Part 2 译者: Fundebug 为了保证可读性,本文采用意译而非直 ...
- 设置dataGridView单元格颜色、字体、ToolTip、字体颜色
this.dataGridView3.Rows[e.RowIndex].Cells["你的那个要判断的列名"].Style.BackColor = Color.MediumPurp ...
- C# % 和 /
/相当于整数除法中的除号,%相当于余号5 除以 2 = 2 余 1,因此 5/2=2,5%2=1.
- Spider-three
一.selenium from selenium import webdriver driver = webdriver.Chrome() # 创建一个chrome浏览器控制对象#driver = w ...
- div中img依据不同分辨率居中显示,超出部分隐藏
在做banner居中时 碰到的问题,知道可以用背景图实现居中显示,但是内心是想深究下的,故找到几种办法收集一下,后面两种真的是奇技淫巧 来着下面两处 https://www.zhihu.com/que ...
- 开发Spring过程中几个常见异常(三):java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.edu.aop.ArithmeticCalculatorImpl at com.edu.aop.Main.main(Main.java:11)
这个异常是在开发Spring案例时遇到的. 贴一下完整异常信息: Exception in thread "main" java.lang.ClassCastException: ...