Codeforces Round #464 (Div. 2) C. Convenient For Everybody 公式推导

C. Convenient For Everybody
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 a1a2, ..., 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
3
1 2 3
1 3
output
3
input
Copy
5
1 2 3 4 1
1 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个时区。现在有一场比赛的起始与终止时间(当地时间)给定,每个时区有若干人想要参加且必须在比赛结束时刻的一个小时或之前加入,要求尽可能使参赛人数更多,求其相对第一时区的开始时间。若有若干答案,取最小输出。

思路:通过分析不难看出,参加的时间为f-s,则通过前缀和数组找出最大值并记录起始的时区位置。再推导出此时第一时区的位置。注意前缀和数组的处理以及最终要输出最小开始时刻。

代码如下:

#include 
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int n,s,f;
ll a[maxn],sum[maxn];

int main()
{
    while (~scanf("%d",&n)){
        sum[0]=0;
        for (int i=1;i<=n;i++){
            scanf("%I64d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        scanf("%d %d",&s,&f);
        int num=f-s;
        int pos=0,ans;
        ll maxv=(ll)0;
        for (int i=num;i<=n+num;i++){
            ll peo;
            pos=i-num;
            if (i<=n)
                peo=sum[i]-sum[i-num];
            else
                peo=sum[n]-sum[i-num]+sum[i-n];

            if (peo>maxv){
                maxv=peo;
                ans=(n-(pos%n)+s)%n;    //由此公式计算出第一时区的时刻
                if (ans==0)
                    ans=n;
            }
            else if (peo==maxv){
                int res=(n-(pos%n)+s)%n;
                if (res==0)
                    res=n;
                ans=min(ans,res);   //取最小起始时刻
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(Codeforces Round #464 (Div. 2) C. Convenient For Everybody 公式推导)