ACM-ICPC2018北京网络赛 80 Days(双端队列+尺取)
题目4 : 80 Days
描述
80 Days is an interesting game based on Jules Verne's science fiction "Around the World in Eighty Days". In this game, you have to manage the limited money and time.
Now we simplified the game as below:
There are n cities on a circle around the world which are numbered from 1 to n by their order on the circle. When you reach the city i at the first time, you will get ai dollars (ai can even be negative), and if you want to go to the next city on the circle, you should pay bi dollars. At the beginning you have c dollars.
The goal of this game is to choose a city as start point, then go along the circle and visit all the city once, and finally return to the start point. During the trip, the money you have must be no less than zero.
Here comes a question: to complete the trip, which city will you choose to be the start city?
If there are multiple answers, please output the one with the smallest number.
输入
The first line of the input is an integer T (T ≤ 100), the number of test cases.
For each test case, the first line contains two integers n and c (1 ≤ n ≤ 106, 0 ≤ c ≤ 109). The second line contains n integers a1, …, an (-109 ≤ ai ≤ 109), and the third line contains n integers b1, …, bn (0 ≤ bi ≤ 109).
It's guaranteed that the sum of n of all test cases is less than 106
输出
For each test case, output the start city you should choose.
提示
For test case 1, both city 2 and 3 could be chosen as start point, 2 has smaller number. But if you start at city 1, you can't go anywhere.
For test case 2, start from which city seems doesn't matter, you just don't have enough money to complete a trip.
- 样例输入
-
2
3 0
3 4 5
5 4 3
3 100
-3 -4 -5
30 40 50 - 样例输出
-
2
-1#include<bits/stdc++.h>
#define MAX 2000010
using namespace std;
typedef long long ll; ll a[MAX],b[MAX];
deque<int> q; int main()
{
int t,n,i,j;
ll x;
scanf("%d",&t);
while(t--){
scanf("%d%lld",&n,&x);
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
}
for(i=n+;i<=n+n;i++){
a[i]=a[i-n];
}
for(i=;i<=n;i++){
scanf("%lld",&b[i]);
}
for(i=n+;i<=n+n;i++){
b[i]=b[i-n];
}
while(q.size()){
q.pop_back();
}
int f=;
for(i=;i<=n+n;i++){
if(x+a[i]-b[i]>=){
x+=a[i]-b[i];
q.push_back(i);
if(q.size()>=n){
printf("%d\n",q.front());
f=;
break;
}
}
else{
while(x+a[i]-b[i]<&&q.size()){
x-=a[q.front()]-b[q.front()];
q.pop_front();
}
if(x+a[i]-b[i]>=){
x+=a[i]-b[i];
q.push_back(i);
if(q.size()>=n){
printf("%d\n",q.front());
f=;
break;
}
}
}
}
if(f==) printf("-1\n");
}
return ;
}
ACM-ICPC2018北京网络赛 80 Days(双端队列+尺取)的更多相关文章
- acm 2015北京网络赛 F Couple Trees 主席树+树链剖分
提交 题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点 解: 我们将第一棵树进行书链剖,然后第 ...
- acm 2015北京网络赛 F Couple Trees 树链剖分+主席树
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- 二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment
题目传送门 /* 题意:问有几个区间最大值-最小值 < k 解法1:枚举左端点,二分右端点,用RMQ(或树状数组)求区间最值,O(nlog(n))复杂度 解法2:用单调队列维护最值,O(n)复杂 ...
- ACM-ICPC 2018年北京网络赛 D-80 days
题意: n个城市环形连接,初始有c的钱,每到i城市,会获得a[i]的金钱,失去b[i]的金钱,问能否走遍这n个城市,且过程中金钱不为负数,输出起始城市,如果答案有多个,输出最小的数字. 思路:a[i] ...
- hihocoder1236(北京网络赛J):scores 分块+bitset
北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...
- 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT
2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
- STL容器:deque双端队列学习
所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...
随机推荐
- IE浏览器的判断
function compatibleIE8(){ var browser = navigator.appName; var b_version = navigator.appVersion; if( ...
- css 字体的unicode码
微软雅黑: YaHei宋体: SimSun黑体: SimHei ;
- CentOS7的/tmp目录自动清理规则(转)
CentOS7的/tmp目录自动清理规则 CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用sys ...
- LeetCode:跳跃游戏【55】
LeetCode:跳跃游戏[55] 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能够到达最后一个位置. 示例 1: 输入: ...
- Mac平台下的抓包神器 —— Charles
在开发界,“抓包”这个词想必大家耳熟能详.通过抓包工具,能够获取设备在网络通讯过程中的交换数据包.在 Windows 平台上,笔者使用较多的是 Fiddler 工具,但是由于 Fiddle 使用 C# ...
- SQL语句编写注意问题
下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍.在这些where子句中,即使某些列存在索引,但是由于编写了劣质的SQL,系统在运行该SQL语句时也不能使用该索引,而同样使用全表扫描 ...
- insert …select …带来的死锁问题
mysql中 insert …select …带来的问题 当使用insert...select...进行记录的插入时,如果select的表是innodb类型的,不论insert的表是什么类型的表,都会 ...
- linux命令学习笔记(33):df 命令
linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了 多少空间,目前还剩下多少空间等信息. .命令格式: df [选项] [文件] .命令 ...
- c ++ auto 的使用
该文转自:https://www.cnblogs.com/KunLunSu/p/7861330.html C++98 auto 早在C++98标准中就存在了auto关键字,那时的auto用于声明变量为 ...
- 【Lintcode】088.Lowest Common Ancestor
题目: Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two n ...