题目4 : 80 Days

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

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 (线段树查询最小值)的更多相关文章

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛D-80 Days--------树状数组

    题意就是说1-N个城市为一个环,最开始你手里有C块钱,问从1->N这些城市中,选择任意一个,然后按照顺序绕环一圈,进入每个城市会有a[i]元钱,出来每个城市会有b[i]个城市,问是否能保证经过每 ...

  2. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解

    题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛

    题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...

  5. hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)

    水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞) #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an int ...

  6. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】

    任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...

  7. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】

    任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...

  8. 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, ...

  9. 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取

    题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...

随机推荐

  1. ArrayList和LinkedList的源码学习,理解两者在插入、删除、和查找的性能差异

    List的使用 List的子类 1). ArrayList 数据结构:数组 2). Vector 数据结构:数组 3). LinkedList 数据结构:循环双向链表 ArrayList .Vecto ...

  2. SCAU-1078 破密-数学的应用

        另外一种方法和该题的题目在这位大佬的博客里 SCAU 1078 破密 还可以参考 https://blog.csdn.net/sinat_34200786/article/details/78 ...

  3. 真的,Kafka 入门一篇文章就够了

    初识 Kafka 什么是 Kafka Kafka 是由 Linkedin 公司开发的,它是一个分布式的,支持多分区.多副本,基于 Zookeeper 的分布式消息流平台,它同时也是一款开源的基于发布订 ...

  4. 网页解析之BeautifulSoup

    介绍及安装 Beautiful Soup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. BeautifulSoup 用来解析 HTML 比较简单,API非常人 ...

  5. java path

    static{ String path = new Object(){ public String getPath() { return this.getClass().getResource(&qu ...

  6. Mysql密码忘记怎么修改?

    做开发的过程中多少会用到MySQL数据库,所以忘记密码也就成为一些马虎的同学的家常便饭了,今天发布一个忘记MySQL密码如何修改的文章作为记录. 1>首先将MySQL的服务关闭,两种方法:1,打 ...

  7. 上手spring boot项目(二)之spring boot整合shiro安全框架

    题记:在学习了springboot和thymeleaf之后,想完成一个项目练练手,于是使用springboot+mybatis和thymeleaf完成一个博客系统,在完成的过程中出现的一些问题,将这些 ...

  8. 淘宝适配 flexible.js

    1.引入 阿里cdm文件,也可以自己下载下来引用,不需要在添加<meta name="viewport"">标签了 <script src=" ...

  9. 数据库MySQL的安装与卸载

    安装 MySQL  卸载 MySQL: 停止 MySQL 服务 开始-->所有应用-->Windows 管理工具-->服务,将 MySQL 服务停止. 卸载 mysql server ...

  10. [TimLinux] TCP全连接队列满

    0. TCP三次握手 该图来自:TCP SOCKET中backlog参数的用途是什么? syns queue: 半连接队列 accept queue: 全连接队列 控制参数存放在文件:/proc/sy ...