In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time "0 hours", instead of it "n hours" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours.
Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are ai people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it.
Help platform select such an hour, that the number of people who will participate in the contest is maximum. Input
The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10 000), where ai is the number of people in the i-th timezone who want to participate in the contest.
The third line contains two space-separated integers s and f (1 ≤ s < f ≤ n). Output
Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them. Examples Input Copy
31 2 31 3
Output
3
Input Copy
51 2 3 4 11 3
Output
4
Note
In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.
In second example only people from the third and the fourth timezones will participate.
大牛的注釋寫的太好了,實在自愧啊~~~~
傳送門
某星球的一天被劃分成n個小時,於是這個星球的世界被劃分成n個時區,標記為1~n。
一個時區的“本地時間”(Local Time)表示為1~n。相鄰兩個時區的“本地時間”間隔為1小時。當1時區的“本地時間”為1時,i時區的“本地時間”為i。
現在舉行一場線上賽事,期間為1小時。在i時區,將有ai人蔘賽。若賽事在“本地時間”s之後開始,且在“本地時間”f之前結束,則該時區的人將參賽,否則不參賽。為使得這個世界的參賽人數最多,求賽事開始的最早時間(表示為1時區的“本地時間”)。
在這個問題中,需要定義一個“標準時間”(Standard Time),之後將所有的“本地時間”折算成“標準時間”。
可以假設一個虛擬0時區,將這個時區的時間定義為“標準時間”。於是:當0時區的“本地時間”(即“標準時間”)為0時,i時區的“本地時間”為i。
設賽事開始的“標準時間”為x,於是:若i時區參賽,則有:
①s-i≤x;
②f-i≥x+1;
於是,參賽時區區間為[s-x..f-x-1]。只需枚舉x,並將對應區間中的參賽人數求和,即可尋找參賽人數的最大值。可以考慮首碼和,或者滑動視窗(註:視窗的滑動方嚮應該是迴圈向左的)。參考程式如下:
#include<bits/stdc++.h>using namespace std;const int N = 100005;int arr[N];int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&arr[i%n]);int s,f;scanf("%d%d",&s,&f);int cur=0;for(int i=s;i<=f;i++) cur+=arr[i%n];int maxt=0,index=-1;for(int i=0;i<n;i++){int l=(s-i+n)%n;int r=(f-i+n)%n;cur-=arr[r];cur+=arr[l];if(cur>maxt){maxt=cur;index=i;}}printf("%d\n",index+1);return 0;}