ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)
#1831 : 80 Days
時間限制:1000ms
單點時限:1000ms
內(nèi)存限制: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直接模擬肯定會TLE,有尺取法,可以減少很多的枚舉
#include<bits/stdc++.h> using namespace std;#define e exp(1) #define pi acos(-1) #define mod 998244353 #define inf 0x3f3f3f3f #define ll long long #define ull unsigned long long #define mem(a,b) memset(a,b,sizeof(a)) int gcd(int a,int b){return b?gcd(b,a%b):a;}const int maxn=2e6+5; int n; ll c,a[maxn],b[maxn],s[maxn]; deque<int> q; int main() {int T;scanf("%d",&T);while(T--){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]);for(int i=n+1; i<=2*n; i++)a[i]=a[i-n],b[i]=b[i-n];while(q.size())q.pop_back();int flag=0;for(int i=1; i<=2*n; i++){if(c+a[i]-b[i]>=0){c+=a[i]-b[i];q.push_back(i);if(q.size()>=n){flag=1;printf("%d\n",q.front());break;}}else {while(c+a[i]-b[i]<0&&q.size()){c-=a[q.front()]-b[q.front()];q.pop_front();}if(c+a[i]-b[i]>=0){c+=a[i]-b[i];q.push_back(i);if(q.size()>=n){flag=1;printf("%d\n",q.front());break;}}}}if(flag==0)puts("-1");}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: The 2018 ACM-ICPC As
- 下一篇: (多表)关联update 超过两个字段