ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-题目4 : 80 Days (模拟+简单数学)

题目4 : 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

题意:

有n个城市,它们按照顺序从1到n编号。当你到达i城市时,你将获得 A[i] 美元(ai甚至可以是负数),如果你想去下一个城市,你应该支付 B[i] 美元。在一开始你有C美元。这个游戏的目标是选择一个城市作为起点,然后沿着圆圈去一次访问所有城市,最后回到起点。 在旅行期间,您所拥有的钱必须不低于零。这里有一个问题:完成旅行,您将选择哪个城市作为开始城市?如果有多个答案,请输出编号最小的城市

思路:

先计算出每一个城市获得的利润(可为负)w[i] 

计算整个过程的所需经费sum+初始金额c sum+c>=0 否则 printf(-1);

再通过单纯的模拟我们可以发现从 a[i] 城市开始游历过程中经费的最小值minn1 与从 a[i+1] 城市开始的minn2 相差一个a[i];

当我们计算a[i+1] 时直接用 minn1-a[i] 判断 minn2>=0 则   printf(i+1);

AC代码:

#include<stdio.h>
using namespace std;
const int maxn = 2e6 + 6;
long long  a[maxn];//经费
long long  b[maxn];//花费
long long  w[maxn];//利润

int n;
long long c;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        long long sum = 0;
        long long sum2 = 0;
        long long minn = 0x3f3f3f3f3f3f;
        long long minn2 = 0x3f3f3f3f3f3f3f;
        int id = 0;
        int id2 = 0;
        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]);
            w[i] = a[i] - b[i];//利润
            sum += w[i];//累加
            if(minn > sum){
                minn = sum;//累加过程中的最小值 相当minn1
                id = i;
            }
        }
        if( c + sum < 0){printf("-1\n");continue;}//不可能
        if(minn + c >= 0){printf("1\n");continue;}//从1开始游历


        for(int i = 1;i <= n;i++)
        {
            minn -= w[i];//从i+1开始游历 相当minn2
            if(minn + c >= 0){//是否可以成功
                    id = i + 1;
            break;//跳出
            }
        }

        printf("%d\n",id);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值