hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞)
描述
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
题意就是一个环,每到一个点+a[i]-b[i],要求整个过程中值>=0,直接维护差值的前缀和就可以。
将环变成2倍长度,然后ST预处理就可以了。
代码:
//D-RMQ(ST)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=2e6+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 ll a[maxn],pre[maxn],mi[maxn][]; void ST(int n)
{
for(int i=;i<=n;i++){
mi[i][]=pre[i];
}
for(int j=;(<<j)<=n;j++){
for(int i=;i+(<<j)-<=n;i++) {
mi[i][j]=min(mi[i][j-],mi[i+(<<(j-))][j-]);
}
}
} ll RMQ(int l,int r)
{
int k=(int)(log(double(r-l+))/log((double)));
return min(mi[l][k],mi[r-(<<k)+][k]);
} int main()
{
int t;
scanf("%d",&t);
while(t--){
int n; ll m;
scanf("%d%lld",&n,&m);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<=n;i++){
ll x;scanf("%lld",&x);
a[i]-=x;a[i+n]=a[i];
}
pre[]=m;
for(int i=;i<=*n;i++){
pre[i]=pre[i-]+a[i];
}
ST(*n);
int ans=-;
for(int i=;i<=n;i++){
ll tmp=RMQ(i,i+n-);
if(tmp-pre[i-]+m>=){
ans=i;
break;
}
}
printf("%d\n",ans);
}
}
。。。
hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)的更多相关文章
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解
题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...
- 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亚洲区预选赛北京赛站网络赛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亚洲区预选赛北京赛站网络赛
题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...
- 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元,问你能否找到一个起点(输出花钱最少的那个) ...
随机推荐
- 爬虫服务集群处理nginx返回504
最近在对爬虫服务做分布式服务的时候总是遇到服务器返回504,搞了两天才发现原来是nginx中有对超时的设置参数,自己都是用默认的,然而客户端的等待时间超过了nginx默认的超时设置 修改 keepal ...
- linux 命令后台运行(转载)
原文连接:https://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html 有两种方式: 1. command & : 后台运行 ...
- CMDB服务器管理系统【s5day88】:采集资产之整合插件
以后导入配置文件不用去from conf而是导入from lib.config,因为在这可以导入global_settings和settings.py import sys import os imp ...
- svg学习
百度百科: SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C制定的一种新的二维矢量图形格式,也 ...
- codevs 1332 上白泽慧音
1332 上白泽慧音 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大 ...
- 梳排序Comb sort
梳排序还是基于冒泡排序,与冒泡不同的是,梳排序比较的是固定距离处的数的比较和交换,类似希尔那样 这个固定距离是待排数组长度除以1.3得到近似值,下次则以上次得到的近似值再除以1.3,直到距离小至3时, ...
- Xamarin入门浅析
1. 安装 1) 使用标准安装流程(JDK1.6 -> Android SDK -> NDK -> Xamarin Studio -> Xamarin Visual Studi ...
- 解决SpringSecurity限制iframe引用页面的问题
使用Spring Security的过程中,需要使用iframe来引入其他域的页面,页面会报X-Frame-Options的错误,试了好几种方法一直未能很好的解决这个问题. 这里涉及到Spring S ...
- mysql 替换
最近贷后好烦,经常让我修改短信模板内容,以前一两个模板手动就直接改了.随着短信模板的增多,手动一个个改内容就不行了. 今天又让我把短信模板中所有的的电话号码修改一下:如:010-44444444改为0 ...
- jQuery实现用户头像裁剪插件cropbox.js
几乎每一个网页是必备图片上传,图片裁剪功能,这里通过cropbox.js插件实现该功能. <script src="js/jquery-1.11.1.min.js">& ...