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 fhours 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.
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 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.
3 1 2 3 1 3
3
5 1 2 3 4 1 1 3
4
题意:这个题意挺绕的,这里定义1-n个时区,和现实中一样,自左向右时区递增,相邻时差1小时。不同的是这里没有0点,用n来代替了,其实方便我们思考了,即:n=24,此时->23点->24点->1点,就是这样变化的。现在给你1-n个时区的不同地方的参赛人数a[i],接着给你s,f:对于任意一个时区,比赛开始时间要不小于s,结束时间要不超过f,这样当地时区的人才可以参加比赛,问你比赛应该在几点开始,能让参赛人数最多。
思路:最重要的一点是:s和f都是针对于第一时区来说的,最后要输出的结果也是第一时区的。所以我们以第一时区为标准,枚举所有的开始时间即1-n,举个例子好理解:n=5时,我规定4点开始比赛,此时时差顺序:4,5,1,2,3,我再规定2点开始比赛:2,3,4,5,1,题目让我们操作的都是第一时区的,之后所有时区都可以确定了。
每确定一次,我们必然可以找一个符合[s,f]的区间,同时利用前缀和,直接sum[r]-sum[l-1](l,r是我们计算出来的区间)求得当前比赛时间下的总人数,枚举n遍求最大值即可。要注意里面左右端点的计算要仔细准确,关于区间端点的计算自己找找规律就行了,实在难以表达了。时间复杂度O(n)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll sum[100010],n,s,f,mmax,p,l,r,tmp,x; //l为左端点,r为右端点
int main()
{
sum[0]=0;
while(~scanf("%lld",&n))
{
for(ll i=1;i<=n;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-1]; //前缀和打表
}
scanf("%lld%lld",&s,&f);
mmax=-1;
for(ll i=1;i<=n;i++)
{
if(s>=i) //如果s大于当前时区,说明需要的区间连续,直接计算即可
{
l=1+s-i; //找出符合s,f的左右端点
r=l+f-s-1;
tmp=sum[r]-sum[l-1];
if(tmp>mmax)
{
mmax=tmp;
p=i;
}
}
else //此时区间可能会断开,例如4->5->1->2
{
l=n-(i-s)+1;
r=l+f-s-1;
if(r>n) //如果断开
{
tmp=sum[n]-sum[l-1]+sum[r-n];
if(tmp>mmax)
{
mmax=tmp;
p=i;
}
}
else //此时没有断开,例如:4->5
{
tmp=sum[r]-sum[l-1];
if(tmp>mmax)
{
mmax=tmp;
p=i;
}
}
}
}
printf("%lld\n",p);
}
return 0;
}