题目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 ;
}

最新文章

  1. webpack初体验
  2. ubuntu下gedit中文乱码
  3. perl常用代码
  4. LSM树——放弃读能力换取写能力,将多次修改放在内存中形成有序树再统一写入磁盘
  5. web2.0、互联网+、IT时代与DT时代、工业4.0 引发的思考
  6. -_-#【Canvas】圆弧运动
  7. 《Effective C++》 阅读小结 (笔记)
  8. c#中queue的用法
  9. leetcode404-----简单的树的遍历
  10. [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
  11. Codeforces Round #520 (Div. 2) B. Math 唯一分解定理+贪心
  12. LabVIEW编程实例:如何通过TCP协议进行数据通信
  13. Python Web简单加法器的实现--Python
  14. One difference between AngularJS&#39; $location and window.location
  15. Informix 常用函数
  16. iOS 9: UIStackView入门
  17. 【洛谷】【动态规划(多维)】P1006 传纸条
  18. 菜鸟vimer成长记——第2.3章、insert模式
  19. [leetcode]Single Number II @ Python
  20. flutter笔记1:VScode安装dart code插件踩坑记录

热门文章

  1. (三十五)golang--面向对象之多态
  2. JVM集训-----内存结构
  3. Java描述设计模式(23):访问者模式
  4. 微信公众号配置及微信jsAPI支付
  5. Java流程控制之(二)循环
  6. 《手把手教你》系列练习篇之3-python+ selenium自动化测试(详细教程)
  7. 在Kubernetes上运行有状态应用:从StatefulSet到Operator
  8. 10个常见的HTTP状态码
  9. PAT-2019年秋季考试-甲级
  10. U盘中病毒无法更改U盘隐藏属性,文件变成快捷方式