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"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...
随机推荐
- ElasticSearch架构思考(转)
一个ElasticSearch集群需要多少个节点很难用一种明确的方式回答,但是,我们可以将问题细化成一下几个,以便帮助我们更好的了解,如何去设计ElasticSearch节点的数目: 打算处理多少数据 ...
- mysql系列之1.mysql基础
非关系型(NOSQL)数据库 键值存储数据库: memcached / redis / memcachedb / Berkeley db 列存储数据库: Cassandra / Hba ...
- 【linux】让普通用户执行root的程序
再有些时候,比如zabbix监控中,需要使用netstat命令查看当前网络链接状态,但是zabbix用户没有权限执行netstat,会导致监控失败,为此使用如下即可解决 chmod +s /bin/n ...
- Java for LeetCode 091 Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- python连接redis并插入url
#!/usr/bin/env python # -*- coding:utf8 -*- import redis ''' 这种连接是连接一次就断了,耗资源.端口默认6379,就不用写 r = redi ...
- 5.2 《锋利的jQuery》jQuery对表格的操作(选项卡/换肤)
表格隔行变色以及单选/复选 表格展开关闭 表格筛选 字体变大/缩小 选项卡 网页换肤 tip1: $("tr:odd")和$("tr:even")选择器索引是从 ...
- 关于Dubbo
什么是Dubbo 一款分布式服务框架 高性能和透明化的RPC远程服务调用方案 SOA服务治理方案 每天为2千多个服务提供大于30亿次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点以及别的公司的业务 ...
- 为什么在实际的 kaggle 比赛中 gbdt 和 random forest 效果非常好?
https://www.zhihu.com/question/51818176/answer/127637712
- Hadoop- Wordcount程序原理及代码实现
如果对Hadoop- MapReduce分布式计算框架原理还不熟悉的可以先了解一下它,因为本文的wordcount程序实现就是MapReduce分而治之最经典的一个范例. 单词计数(wordcount ...
- 分享知识-快乐自己:论 Mybatis中的关联关系(一对多,多对一,多对多)
论:一对多:(举例一个省有多个市)就是实体类中有(市)类型集合属性:多对一:(多个市有一个共同的省)就是类中有(省)类型的属性.下面来介绍:一对一.多对一的使用方式. 一对多方: package ml ...