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

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

const 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

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值