2018北京网络赛D 80days (尺取)

2018-10-06 08:06:00来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

#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


比赛的时候都以为是dp,但是自己写的感觉就是暴力。。。

思路:p[i]=a[i]-b[i] 化环为直 尺取法取长度为n的序列,front如果大于n就是-1 (见注释)

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <sstream>
13 #include <stack>
14 using namespace std;
15 #define rep(i,a,n) for (int i=a;i<n;i++)
16 #define per(i,a,n) for (int i=n-1;i>=a;i--)
17 #define pb push_back
18 #define mp make_pair
19 #define all(x) (x).begin(),(x).end()
20 #define fi first
21 #define se second
22 #define SZ(x) ((int)(x).size())
23 #define FO freopen("in.txt", "r", stdin);
24 #define debug(x) cout << "&&" << x << "&&" << endl;
25 #define lowbit(x) (x&-x)
26 #define mem(a,b) memset(a, b, sizeof(a));
27 typedef vector<int> VI;
28 typedef long long ll;
29 typedef pair<int,int> PII;
30 const ll mod=1000000007;
31 const int inf = 0x3f3f3f3f;
32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
34 //head
35 
36 const int N=1e6+5;
37 int p[N<<1],a[N],b[N],_,n;
38 ll c;
39 int main() {
40     for(scanf("%d",&_);_;_--) {
41         scanf("%d%lld",&n,&c);
42         rep(i,1,n+1) scanf("%d",&a[i]);
43         rep(i,1,n+1) scanf("%d",&b[i]);
44         rep(i,1,n+1) p[i]=p[i+n]=a[i]-b[i];//模拟环
45         int front=1,rear=1;//都指向1
46         while(front<=n&&rear-front+1<=n) {//front<=n n长度
47             c+=p[rear];//累加 后移
48             rear++;
49             while(c<0){//如果c<0 说明front不能到达rear front后移
50                 c-=p[front];
51                 front++;
52             }
53         }
54         if(front>n) puts("-1");//1~n都不行
55         else printf("%d\n",front);//可以取长度为n的序列 输出队头
56     }
57 }

 

(貌似提交是过不去的。。。)


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:学c++有感

下一篇:实验1 查看CPU和内存,用机器指令和汇编指令编程