hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)

 

水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞)

 

时间限制: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

 

 

题意就是一个环,每到一个点+a[i]-b[i],要求整个过程中值>=0,直接维护差值的前缀和就可以。
将环变成2倍长度,然后ST预处理就可以了。

 

代码:

 1 //D-RMQ(ST)
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<bitset>
 7 #include<cassert>
 8 #include<cctype>
 9 #include<cmath>
10 #include<cstdlib>
11 #include<ctime>
12 #include<deque>
13 #include<iomanip>
14 #include<list>
15 #include<map>
16 #include<queue>
17 #include<set>
18 #include<stack>
19 #include<vector>
20 using namespace std;
21 typedef long long ll;
22 
23 const double PI=acos(-1.0);
24 const double eps=1e-6;
25 const ll mod=1e9+7;
26 const int inf=0x3f3f3f3f;
27 const int maxn=2e6+10;
28 const int maxm=100+10;
29 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
30 #define lson l,m,rt<<1
31 #define rson m+1,r,rt<<1|1
32 
33 ll a[maxn],pre[maxn],mi[maxn][25];
34 
35 void ST(int n)
36 {
37     for(int i=1;i<=n;i++){
38         mi[i][0]=pre[i];
39     }
40     for(int j=1;(1<<j)<=n;j++){
41         for(int i=1;i+(1<<j)-1<=n;i++) {
42             mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
43         }
44     }
45 }
46 
47 ll RMQ(int l,int r)
48 {
49     int k=(int)(log(double(r-l+1))/log((double)2));
50     return min(mi[l][k],mi[r-(1<<k)+1][k]);
51 }
52 
53 int main()
54 {
55     int t;
56     scanf("%d",&t);
57     while(t--){
58         int n; ll m;
59         scanf("%d%lld",&n,&m);
60         for(int i=1;i<=n;i++)
61             scanf("%lld",&a[i]);
62         for(int i=1;i<=n;i++){
63             ll x;scanf("%lld",&x);
64             a[i]-=x;a[i+n]=a[i];
65         }
66         pre[0]=m;
67         for(int i=1;i<=2*n;i++){
68             pre[i]=pre[i-1]+a[i];
69         }
70         ST(2*n);
71         int ans=-1;
72         for(int i=1;i<=n;i++){
73             ll tmp=RMQ(i,i+n-1);
74             if(tmp-pre[i-1]+m>=0){
75                 ans=i;
76                 break;
77             }
78         }
79         printf("%d\n",ans);
80     }
81 }

 

 

 

。。。

 

posted @ 2018-10-08 19:52  ZERO-  阅读(321)  评论(0编辑  收藏  举报