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.
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
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个时区,每个时区都有一些人。现在举办一场为时一个小时的比赛,给定一个时间区间[s,f]。本地时间在[s,f-1]之内的人会参加比赛,比赛方希望参加的人越多越好。求出最多人参加的时候,第一个时区的本地时间是多少。
思路:因为随着时间的推移,每个时区的时间都会随之改变。我们要找到长度为f-s的和最大的连续子串,这里要注意因为是时区,所以首尾可以说是“相连”的,刚开始用笨方法来处理发现想的有点头大,最后看了大佬的处理过程才发现用模运算就非常简洁了。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
int n,i,s,f,a[100005];
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i%n]);//这里的模运算有些巧妙,之所以如此是方便了后面区间向左滑动的处理
scanf("%d %d",&s,&f);
int ans=0,maxn=0,pos=0;
for(i=s;i<f;i++)
ans+=a[i%n];//先求区间[s,f-1]的和
maxn=ans;
int l,r;
for(i=1;i<n;i++)
{
//区间向左滑动,这样处理时区问题较为方便
l=(s-i+n)%n;
r=(f-i+n)%n;
ans+=a[l];
ans-=a[r];
if(ans>maxn)
{
maxn=ans;
pos=i;
}
}
printf("%d\n",pos+1);
return 0;
}
顺便附上java的代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin= new Scanner(System.in);
int n,s,f,i;
int[] a=new int[100005];
while(cin.hasNext()) {
n=cin.nextInt();
for(i=1;i<=n;i++)
a[i%n]=cin.nextInt();
s=cin.nextInt();
f=cin.nextInt();
int ans=0,maxn=0,pos=0;
for(i=s;i<f;i++)
ans+=a[i%n];
maxn=ans;
int l,r;
for(i=1;i<n;i++) {
l=(s-i+n)%n;
r=(f-i+n)%n;
ans+=a[l];
ans-=a[r];
if(ans>maxn) {
maxn=ans;
pos=i;
}
}
System.out.printf("%d\n", pos+1);
}
}
}