2018北京icpc网络赛-D 80 Days【前缀和+multiset】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013852115/article/details/82822398

#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

思路:

很容易想到对数组进行倍增。

用一个数组dis对a[i]-b[i]进行前缀和,然后问题可以转化为找到一个区间,区间内的最小dis+c只要非负,就是一个解。

而求正确的dis时,需要利用前缀和的思想。整体过程类似与一个滑动窗口,可以用一个multiset维护窗口内的最小值。

比赛时读错题意,手动把b数组往前错了一位,导致无限wa。。。

思路:

#include<bits/stdc++.h>
using namespace std;
#define MAXN 2000005
#define ll long long
int n;
ll c,a[MAXN],b[MAXN],dis[MAXN];
multiset<ll> vec;
multiset<ll>::iterator it;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&n,&c);
        for(int i=0;i<n;i++)
            scanf("%lld",a+i);
        for(int i=0;i<n;i++)
            scanf("%lld",b+i);
        for(int i=n;i<2*n;i++)
        {
            a[i]=a[i-n];
            b[i]=b[i-n];
        }
        dis[0]=a[0]-b[0];
        for(int i=1;i<2*n;i++)
        {
            dis[i]=dis[i-1]+a[i]-b[i];
        }
        vec.clear();
        for(int i=0;i<n;i++)
            vec.insert(dis[i]);
        int flag=0;
        if((*vec.begin())+c>=0)
        {
            printf("%d\n",1);
            flag=1;
            continue;
        }
        for(int i=1;i<n;i++)
        {
            it=vec.find(dis[i-1]);
            vec.erase(it);
            vec.insert(dis[i+n-1]);
            if((*vec.begin())+c-dis[i-1]>=0)
            {
                printf("%d\n",i+1);
                flag=1;
                break;
            }

        }
        if(!flag) printf("-1\n");
    }
    return 0;
}
/*
1
13 9
2 -6 -3 -3 0 1 9 0 -4 -4 1  -4 6
4 1  -3 -5 9 4 1 -1 0 6  -9 -3 -2
*/

猜你喜欢

转载自blog.csdn.net/u013852115/article/details/82822398