【HihoCoder - 1831】80 Days(尺取 或 线段树)
題干:
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.
Input
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
Output
For each test case, output the start city you should choose.
Sample Input
2 3 0 3 4 5 5 4 3 3 100 -3 -4 -5 30 40 50Sample Output
2 -1Hint
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.
題目大意:
有n個(gè)城市圍成一圈 編號(hào)為1-n。第一次到達(dá)第i個(gè)城市需要獲得ai金幣,同時(shí)如果想從當(dāng)前城市前往其他城市,需要花費(fèi)bi金幣。起初有c金幣??梢匀我膺x擇一個(gè)城市作為起點(diǎn),問(wèn)能否從一個(gè)城市出發(fā)環(huán)游一圈?【起點(diǎn)等于經(jīng)過(guò)了兩次】
解題報(bào)告:
剛開(kāi)始讀錯(cuò)題了,以為是從每個(gè)點(diǎn)可以走到任意一個(gè)合法的點(diǎn),但是其實(shí)是1~n圍成一個(gè)圈,所以必須順著走。這就更簡(jiǎn)單了。
? 數(shù)據(jù)水了,,按題目意思來(lái)說(shuō),應(yīng)該特判n==1這種情況?而且尺取的話少寫一句維護(hù)左端點(diǎn)也能過(guò)?
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e6 + 5; int n; ll c,a[MAX],b[MAX]; int main() {int t;cin>>t;while(t--) {scanf("%d%lld",&n,&c);for(int i = 1; i<=n; i++) cin>>a[i],a[i+n] = a[i];for(int i = 1; i<=n; i++) cin>>b[i],b[i+n] = b[i];ll x = 0;int l = 1,ans = -1;for(int r = 1; r<=2*n; r++) {x += a[r]-b[r];while(x < -c && l <= r) {x -= a[l]-b[l];l++; }while(r-l+1 > n) x -= a[l]-b[l],l--; //為什么少寫這一句也能過(guò)?if(r-l+1 == n && x >= -c) {ans = l;break;}}printf("%d\n",ans);}return 0 ; }線段樹(shù)代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e6 + 5; struct TREE {int l,r;ll minn; } tr[MAX<<2]; int n; ll c; ll a[MAX],b[MAX]; void pushup(int cur) {tr[cur].minn = min(tr[cur*2].minn,tr[cur*2+1].minn); } void build(int l,int r,int cur) {tr[cur].l = l;tr[cur].r = r;if(l == r) {tr[cur].minn = a[l];return;}int m = (l+r)>>1;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur); } ll query(int pl,int pr,int cur) {if(pl <= tr[cur].l && pr >= tr[cur].r) {return tr[cur].minn;} ll res = 1e15;if(pl <= tr[cur*2].r) res = query(pl,pr,cur*2);if(pr >= tr[cur*2+1].l) res = min(res,query(pl,pr,cur*2+1));return res; }int main() {int T;cin>>T;while(T--) {scanf("%d%lld",&n,&c);for(int i = 1; i<=n; i++) scanf("%lld",a+i),a[i+n] = a[i];for(int i = 1; i<=n; i++) scanf("%lld",b+i),b[i+n] = b[i];if(n == 1) {if(c-a[1]>=0) printf("1\n");else printf("-1\n");continue; }for(int i = 1; i<=2*n; i++) a[i] -= b[i],a[i]+=a[i-1];build(1,2*n,1);int ans = -1;for(int i = 1; i<=n; i++) {ll x = query(i,i+n-1,1)-a[i-1];if(x >= -c) {ans = i;break;}}printf("%d\n",ans);} return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【HihoCoder - 1831】80 Days(尺取 或 线段树)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最新版民间借贷新规正式实施,哪些钱可以不
- 下一篇: 【 HDU - 5459】Jesus I