2018-ACM/ICPC北京网络赛D题 80 Days(非暴力0(n)解法)

描述

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 ≤ 10^6, 0 ≤ c ≤ 10^9). The second line contains n integers a1, …, an (-10^9 ≤ ai ≤ 10^9), and the third line contains n integers b1, …, bn (0 ≤ bi ≤ 10^9).

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.

利用前缀和与后缀和优化dp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL maxn = 1e6+7;
LL a[maxn],b[maxn],cha[maxn],qian[maxn],qian2[maxn], hou[maxn], hou2[maxn];
void init(LL n,LL c){
hou[n+1] = qian[0] = 0;
hou2[n] = hou[n] = cha[n];
qian2[1] = qian[1] = cha[1];
for(int i = 2;i<=n;i++){
qian[i] = cha[i]+qian[i-1];
qian2[i] = min(qian[i],qian2[i-1]);
}
LL sum = 0;
for(int i = n-1;i>0;i--){
hou[i] = cha[i]+hou[i+1];
hou2[i] = min(hou[i]-sum,cha[i]);
if(hou2[i]>=0){
sum=hou[i];
hou2[i]=0;
}
}
}
int fun(LL n,LL c){
if(qian[n]+c<0)
return -1;
for(int i = 1;i<=n;i++){
if(cha[i]+c>=0&&cha[i]+c+hou2[i+1]>=0){
if(hou[i]+c+qian2[i-1]>=0){
return i;
}
}
}
return -1;
}
int main(){
int t;
LL n,c;
scanf("%d",&t);
while(t--){
scanf("%lld%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]);
cha[i] = a[i] - b[i];
}
init(n,c);
int tmp = fun(n,c);
printf("%d\n",tmp);
}
return 0;
}

尺取法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#define MAX 1000005

int p[MAX<<1],a[MAX],b[MAX];

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
long long c;
scanf("%d%lld",&n,&c);
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
}
for(int i = 1;i <= n;i++)
{
scanf("%d",&b[i]);
}
for(int i = 1;i <= n;i++)
{
p[i] = p[i + n] = a[i] - b[i];
}
int l = 1,r = 1;
while(l <= n && r - l + 1 <= n)
{
c = c + p[r];
r ++;
while(c < 0)
{
c = c - p[l];
l ++;
}
}
if(l > n)
printf("-1\n");
else
printf("%d\n",l);
}
}

纸短情长啊文章结束了但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!