ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 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
题目大意:
给你初始资金c,再告诉你n个围成一圈(有序)的地点。每到一个地点,你可以得到ai元钱,然后你必须到下一个地点,这样你需要支出bi元。路途中不可以有一个时刻你的资金为负值。问字典序最小的起点,使得可以环游一周。
首先每个地点的收益是wi=ai-bi,开辟一个数组保存w1w2...wn-1wnwn-1...w2w1。
计算wi数组的前缀和,这样就可以O(1)地得到某段的收益了。
顺序检索wi到wi+n-1的最小值与wi-1的差,若大于等于-c,就可以输出答案了。
用线段树查询区间最小值。
#include<cstdio>
#include<queue>
#include<algorithm> using namespace std; const int maxn=;
const int inf=; int a[maxn*+]; struct ttree
{
int l,r;
int mmin;
};
ttree tree[maxn**+]; void pushup(int x)
{
if(tree[x].l==tree[x].r)
return;
tree[x].mmin=min(tree[x*].mmin,tree[x*+].mmin);
} void build(int x,int l,int r)
{
tree[x].l=l;
tree[x].r=r;
if(l==r)
{
tree[x].mmin=a[l];
}
else
{
int mid=(l+r)/;
build(x*,l,mid);
build(x*+,mid+,r);
pushup(x);
}
} int query(int x,int l,int r)
{
if(l<=tree[x].l&&r>=tree[x].r)
return tree[x].mmin;
int ret=inf;
int mid=(tree[x].l+tree[x].r)/;
if(l<=mid)
ret=min(ret,query(x*,l,r));
if(r>mid)
ret=min(ret,query(x*+,l,r));
return ret;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,c;
scanf("%d%d",&n,&c);
for(int i=;i<=n;i++)
scanf("%d",a+i);
for(int i=,b;i<=n;i++)
{
scanf("%d",&b);
a[i]-=b;
}
for(int i=;i<n;i++)
a[n+i]=a[i]; for(int i=;i<n*;i++)
a[i]+=a[i-]; a[]=;
int ans=-;
build(,,n*-);
for(int i=;i<=n;i++)
if(query(,i,i+n-)-a[i-]+c>=)
{
ans=i;
break;
}
printf("%d\n",ans);
}
return ;
}
ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)的更多相关文章
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛D-80 Days--------树状数组
题意就是说1-N个城市为一个环,最开始你手里有C块钱,问从1->N这些城市中,选择任意一个,然后按照顺序绕环一圈,进入每个城市会有a[i]元钱,出来每个城市会有b[i]个城市,问是否能保证经过每 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解
题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛
题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...
- hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞) #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an int ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】
任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】
任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...
- 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取
题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...
随机推荐
- Centos下的MySQL安装及配置
里使用的是VMware虚拟机和Centos7系统 虚拟机安装这里不多讲,网上教程很多了,这里就介绍下虚拟机的网络配置. 虚拟机网络配置 Centos网络连接模式这里设置为桥接模式,不用勾选复制物理网络 ...
- 三种方法教你HTML实现点击某一个元素之外触发事件
HTML实现点击某一个元素之外触发事件 大致编写的HTML界面渲染后是这个样子的,我们现在想要实现的需求是点击Button所在的div不会触发事件,而在点击Button所在的div之外的区域时会触发事 ...
- wordpress开源小程序
wordpress多端开源小程序正式发布了,目前支持微信/QQ/百度/今日头条. 目前我们的开源小程序,已经建立了微信QQ交流群,需要的可以加下,微信添加hackdex(备注开源拉你入群),QQ群:7 ...
- VS Code 中文社区正式成立啦!VS Code Day 圆满落幕!
背景简介 Visual Studio Code 是一款现代化轻量级代码编辑器,它免费.开源.跨平台.功能强大.本次 VS Code Day 是广大 VS Code 爱好者一起学习与交流的盛会,让我们对 ...
- Glibc编译报错:*** These critical programs are missing or too old: as ld gcc
Binutils版本升级 这里是binutils版本过低导致, 查看已部署版本 上传离线升级包 [root@sdw1 glibc]# tar -zxvf binutils-2.32.tar.gz [r ...
- linux中Nginx安装
linux中Nginx安装 编译安装 Nginx的优点太多,这里不再赘述,详情请看这篇博客深入理解nginx. Nginx的安装有rpm包安装.编译安装和docker安装.本文将介绍编译安装方 ...
- Linux上ES单机版安装
设置 IP 地址 vi /etc/sysconfig/network-scripts/ifcfg-ens32 重启网卡 [root@localhost ~] systemctl restart n ...
- 100天搞定机器学习|Day57 Adaboost知识手册(理论篇)
Boosting算法 Boosting是一种用来提高弱分类器准确度的算法,是将"弱学习算法"提升为"强学习算法"的过程,主要思想是"三个臭皮匠顶个诸葛 ...
- Spring(Bean)2
<!-- util:list封装的心 --> <bean id="personList2" class="spring.beans.di.collect ...
- Process用法与进程详解
僵尸与孤儿进程 僵尸进程:父进程的子进程结束的时候父进程没有wait()情况下子进程会变成僵尸进程 孤儿进程(无害) 一个父进程退出,而它的一个或多个子进程还在运行,那么那些子进程将成为孤儿进程.孤儿 ...