#1831 : 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

直接模拟肯定会TLE,有尺取法,可以减少很多的枚举

#include<bits/stdc++.h>
using namespace std;#define e exp(1)
#define pi acos(-1)
#define mod 998244353
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}const int maxn=2e6+5;
int n;
ll c,a[maxn],b[maxn],s[maxn];
deque<int> q;
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d%lld",&n,&c);for(int i=1; i<=n; i++)scanf("%lld",&a[i]);for(int i=1; i<=n; i++)scanf("%lld",&b[i]);for(int i=n+1; i<=2*n; i++)a[i]=a[i-n],b[i]=b[i-n];while(q.size())q.pop_back();int flag=0;for(int i=1; i<=2*n; i++){if(c+a[i]-b[i]>=0){c+=a[i]-b[i];q.push_back(i);if(q.size()>=n){flag=1;printf("%d\n",q.front());break;}}else {while(c+a[i]-b[i]<0&&q.size()){c-=a[q.front()]-b[q.front()];q.pop_front();}if(c+a[i]-b[i]>=0){c+=a[i]-b[i];q.push_back(i);if(q.size()>=n){flag=1;printf("%d\n",q.front());break;}}}}if(flag==0)puts("-1");}return 0;
}

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

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days ——尺取

    描述 80 Days is an interesting game based on Jules Verne's science fiction "Around the World in E ...

  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. 优化网站设计方案提升网站用户回头率
  2. 基于numpy实现线性回归问题分析
  3. PostgreSQL数据库配置网络访问
  4. 程序设计与算法----动态规划之最长上升子序列
  5. 2.5.1.2、ImportBeanDefinitionRegistrar 注册BeanDefinition
  6. JS实现图片翻书效果
  7. C++中用stringstream类进行数据类型的转换
  8. c语言网格搜索,基于C
  9. 生日快乐程序_这家线下服装店,靠小程序做活动7天就获客6万
  10. gmx一定要在linux下运行么,gmx_mmpbsa使用说明
  11. 人民币对美元汇率中间价报6.7969元 下调115个基点
  12. 过拟合与欠拟合及解决方法
  13. 使用树莓派搭建直播平台实现b站实时直播
  14. 分类计数原理与分步计数原理_分类加法计数原理与分步乘法计数原理的解题策略之一...
  15. 零基础 · 传说之下同人游戏制作教程
  16. smarty实例教程一
  17. KVM虚拟化配置详解
  18. 1024程序员节!!
  19. excel一列数字前面批量加个逗号
  20. android开发笔记之网络编程—简易新闻客户端

热门文章

  1. 淘淘商城 @Autowired 装配失败
  2. 2017年4月11日
  3. CentOS 下安装
  4. Github上的资源清单
  5. 根据工作年限预测工资python代码实现
  6. Python学习笔记:循环语句
  7. 视频分类/动作识别数据库研究现状
  8. 【笔记】An explainable deep machine vision framework for plant stress phenotyping
  9. Opencv模块功能介绍
  10. ustc小道消息20211213