欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-10 12:50:59
#1831 : 80 Days #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Verne's science fiction "Around th ......

#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 }

 

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