ACM _ICPC2018北京网络赛 80 Days(签到题)

#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

 

感觉好像是水题,但模拟了老半天,果然我是蒟蒻啊,嗷嗷嗷地~被打自闭

#include
#define rep(i,j,k) for(int i=j;i>T;
    while(T--)
    {
        int n;
        ll st;
        cin>>n>>st;
        rep(i,0,n)
        {
            cin>>a[i];
            a[i+n] = a[i];
        }
        ll b;
        rep(i,0,n)
        {
            cin>>b;
            c[i] = a[i]-b;
            c[i+n] = c[i];
        }
        sum[0] = 0;
        for(int i=1;i<=2*n;i++)
        {
            sum[i]=sum[i-1]+c[i-1];  //前缀和
        }
        if(sum[n] + st < 0)
        {
            cout<<"-1"<=0)
            {
                if(ans == 0) ans = i;//保留ans队列最小的下标即是答案
                if(ans > n) break;   //去重
                tot++;
                if(tot==n) {
                    flag = 1;
                    break;
                }
            }
            else
            {
                if(!tot)
                {
                    ans = 0;
                    prifix = i ;
                    continue;
                }
                int cur = ans;
                while(tot && sum[i] - sum[cur] + st < 0) { tot-- , cur++;}

                if(!tot)
                {
                    ans = 0;
                    prifix = i ;
                }

                else
                {
                    ans = cur + 1;
                    //tot--;      //额外增加了i,总数tot不用再减少
                    if(ans > n) break;   //去重
                    prifix = cur;
                }

            }
        }

        if(!flag) cout<<"-1"<

 

你可能感兴趣的:(易,前缀和)