日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HihoCoder - 1831】80 Days(尺取 或 线段树)

發布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 50

Sample Output

2 -1

Hint

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個城市圍成一圈 編號為1-n。第一次到達第i個城市需要獲得ai金幣,同時如果想從當前城市前往其他城市,需要花費bi金幣。起初有c金幣??梢匀我膺x擇一個城市作為起點,問能否從一個城市出發環游一圈?【起點等于經過了兩次】

解題報告:

剛開始讀錯題了,以為是從每個點可以走到任意一個合法的點,但是其實是1~n圍成一個圈,所以必須順著走。這就更簡單了。

? 數據水了,,按題目意思來說,應該特判n==1這種情況?而且尺取的話少寫一句維護左端點也能過?

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--; //為什么少寫這一句也能過?if(r-l+1 == n && x >= -c) {ans = l;break;}}printf("%d\n",ans);}return 0 ; }

線段樹代碼:

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

?

總結

以上是生活随笔為你收集整理的【HihoCoder - 1831】80 Days(尺取 或 线段树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。