描述
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

题意:

一个人从1旅行到n再回到1,相当于一个环,任何地方都可以是起点,那个人第一次到一个地方可以得到a[i]元钱,从这个地方到下一个地方需要用到b[i]元钱,他一开始有k元钱,问你初始地的最少序号是多少,如果不能旅行则输出-1。

题解:

尺取法,如果右端点的值小于0的话就移左边,否则移右端点。

#include<stdio.h>
using namespace std;
#define LL long longconst int N=1e6+5;LL ab[2*N],a[2*N];
LL mo;int ans;int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d%lld",&n,&mo);for(int i=1;i<=n;i++){scanf("%lld",&a[i]);}for(int i=1;i<=n;i++){LL tmp;scanf("%lld",&tmp);ab[i]=a[i]-tmp;}for(int i=n+1;i<=2*n;i++){a[i]=a[i-n];ab[i]=ab[i-n];}int l=1,r=0;LL now=mo;int flag=0;while(1){while(now>=0&&r<=2*n){if(r>=l+n-1){flag=1;break;}r++;now+=ab[r];}if(flag){printf("%d\n",l);break;}while(now<0&&l<=r){l++;now-=ab[l-1];}if(l>n){printf("-1\n");break; }}}
}/*
111
7 0
-2 0 2 1 -7 6 2
0 0 0 0 0 0 0
7 6
-2 0 2 1 -7 6 2
0 0 0 0 0 0 0
3 18
-6 -6 -6
0 0 0*/

ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days ——尺取相关推荐

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)

    #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...

  2. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 【bfs + 记忆化搜索 + 剪枝】 AC 代码

    ACM 北京区域赛 bfs+剪枝+ms 第一个一遍过的题目,基本没有看题解 记忆搜索当中,注意初始化成一个特殊值:而在访问之后,每个点就会有一个不同于INF(或者 -1等特殊标记)的值 先到先得,适者 ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 Tomb Raider(map+二进制枚举)

    #1829 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daugh ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D【队列】

    #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...

  5. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D. 80 Days

    题解 题目大意 n个点组成一个环形 初始钱为m 从i走到j需要-b[i] + a[j] 要求按照顺时针走完所有的点(不用再回到起点) 过程中m不能小于0 输出最小的起点编号 直接把a[i]和b[i]合 ...

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

    Saving Tang Monk II Tomb Raider Cheat 80 Days Odd Chess Shortest Path Problem The Mole K-Dimensional ...

  7. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A Saving Tang Monk II【分层bfs】

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also <Monkey>) is one of the ...

  8. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A. Saving Tang Monk II

    题解 题目大意 给一个图S是起点 T是终点 .是空房间 #是毒气室 B是氧气瓶存放室 P是加速室 每次走到空房间或者起点消耗1秒 走到氧气室获得一个氧气瓶最多携带5个氧气瓶 进入毒气室需要一瓶氧气并且 ...

  9. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 C-cheat

    超级水的模拟的,我也就会做模拟了--坑有点多,一直没调出来bug,赛后才发现少了一个数字-- 描述 Cheat is a card game played by four players sittin ...

最新文章

  1. 用python打开视频_Python读取视频的两种方法(imageio和cv2)
  2. windows安装nvm
  3. 局部放大_Origin教程|巧用ZOOM功能做数据对比和快速绘制局部放大图
  4. python编程30题_python编程30个常用技巧
  5. 中国为什么不能成为国际数据中心枢纽
  6. Python实现WGS 84坐标与web墨卡托投影坐标的转换
  7. Openlayers 杂项
  8. 如何阅读Java源码?
  9. 出圈!迅镭激光切割设备亮相热播剧《麓山之歌》
  10. SHOI 2008 仙人掌图 BZOJ 1023
  11. JS_实现图片的自动轮播
  12. 四类九种移位寄存器总结(循环(左、右、双向)移位寄存器、逻辑和算术移位寄存器、串并转换移位寄存器、线性反馈移位寄存器LFSR|verilog代码|Testbench|仿真结果)
  13. 匕年级下册计算机计划,七年级下学期班主任工作计划
  14. JDK8新特性:Lambda表达式、Stream流、日期时间工具类
  15. java语音播报源代码_详解Android 语音播报实现方案(无SDK)
  16. 中山大学计算机类专业代码,各大学代码及专业代码
  17. js判断json有没有某值_JS中判断JSON数据是否存在某字段的方法 JavaScript中判断json中是否有某个字段...
  18. BT profile
  19. stm32使用PWM时,关闭PWM引脚会出现高电平解决方案
  20. PyTorch核心加速技术涉嫌抄袭?MIT教授创业公司将Facebook告上法庭

热门文章

  1. Scoop包管理器的安装和相关技巧
  2. 程序员年终总结怎么写
  3. 元气森林、波士顿咨询、毕马威等 20+ 嘉宾齐聚观远数据2022智能决策峰会
  4. V神diss「中本聪」!!又有好戏看咯
  5. 热力学分布用matlab,热力学matlab
  6. 《可以量化的经济学》出版了!nbsp;请…
  7. 高并发系统设计 --基于bitmap的用户签到
  8. 奇安信、深信服、启明星辰的安全红利战
  9. TouchScreen驱动
  10. 三坐标测圆的直径怎么测_三坐标检测大半径小圆弧的实用方法