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 1062:分段函数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3306 解决:1952 题目描述: 编写程序,计算下列分段函数y=f(x)的值. y=-x+2.5; 0<=x<2 y=2-1. ...
- 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线
如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...
- javascript实现日期时间动态显示
.aspx代码例如以下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def ...
- 链表的C++实现
有的时候,处于内存中的数据并非连续的.那么这时候.我们就须要在数据结构中加入一个属性.这个属性会记录以下一个数据的地址.有了这个地址之后.全部的数据就像一条链子一样串起来了,那么这个地址属性就起到 ...
- 解决MAC Appium设备连不上IOS的的问题'idevice_id' program is not installed
解决MAC Appium设备连不上IOS的的问题 错误的: [XCUITest] The 'idevice_id' program is not installed. If you are runni ...
- 爬虫前戏(回顾掌握) -- HTTP和HTTPS
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- packstack快速安装
1 安装软件库 更新安装的软件包,命令如下: sudo yum update -y 建立RDO库,命令如下: sudo yum install -y https://rdo.fedorapeople. ...
- 重新实践c++primer上面的代码
又重新敲了敲c++primer上面的代码,觉得很有意思,讲的很细,c++真牛逼啊 #include <iostream> #include <string> #include ...
- mini2440 u-boot禁止蜂鸣器
mini2440的u-boot版本启动之后马上就会开启蜂鸣器,在办公环境下有可能会影响同事的工作,所以我考虑将其禁止掉. 我使用的mini2440使用的光盘是2013年10月的版本,我在该光盘下的u- ...
- Spring Boot2.0之自定义参数
自定义参数,把不同环境的配置放到配置文件中去. 不同环境,如何区分配置文件信息,自定义配置文件信息 比如在 application.properties定义个参数 name=toov5 Spring ...