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"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...
 
随机推荐
- 九度OJ 1017:还是畅通工程 (最小生成树)
			
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4789 解决:2382 题目描述: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程&quo ...
 - 【24题】P2766最长不下降子序列问题
			
网络流二十四题 网络流是个好东西,希望我也会. 网络流?\(orz\ zsy!!!!!\) P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(d ...
 - Yii 框架 URL路径简化
			
Yii 框架的訪问地址若不简化会让人认为非常繁琐.未简化的地址一般格式例如以下: http://localhost:80/test/index.php?r=xxx/xxx/xxx 若是带有參数会更复杂 ...
 - MV45AOZZ 销售订单增强点
			
[转自 http://blog.csdn.net/zhongguomao/article/details/6712580]choose the table VBAP or VBAK ( dependi ...
 - Map总结--HashMap/HashTable/TreeMap/WeakHashMap使用场景分析(转)
			
首先看下Map的框架图 1.Map概述 1.Map是键值对映射的抽象接口 2.AbstractMap实现了Map中绝大部分的函数接口,它减少了“Map实现类”的重复编码 3.SortedMap有序的“ ...
 - wireshark 学习 3 display filter
			
过滤信息,得到想要的帧进行分析. http://www.networkcomputing.com/networking/wifi-troubleshooting-using-wireshark/155 ...
 - spring-boot3代码
			
App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...
 - Appium移动自动化配置-ios&安卓
			
官网安装参考:https://www.npmjs.com/package/appium Appium安装 1.安装nodejs 2.安装appium iOS侧环境安装 1.安装xcode 2.安装Xc ...
 - Django+ajax+jsonp实现借口调用文本处理
			
首页 提交 <script src="/static/jquery-2.1.4.min.js"></script> <script type=&quo ...
 - Contiki Timer & Stimer 模块
			
一.Timer API struct timer { clock_time_t start; clock_time_t interval; }; CCIF void timer_set(struct ...