本文主要是介绍CodeForces 939C Convenient For Everybody(前缀和+滑动窗口),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门
题目描述:
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.
题目翻译:
有n个时区,每个时区一天有n个小时。现在要举办一场持续1小时的比赛,开始时间与某个小时的开始时间一致。 在第i时区,有a[i]人参赛。
来自一个时区的人只会参加开始时间不早于当地时间s小时,结束时间不晚于当地时间f小时的比赛。
求出可以使得参赛人数最多的时间。
第一行,输入一个整数n(2≤n≤100000)。
第二行,输入n个整数(1≤ai≤10000)。
第三行,输入两个整数,s和f。
思路:
设比赛在1时区的开始的时间为x,则如果i时区参赛,满足于以下条件:
1.
2.
所以,参赛时区的区间为
现在只需枚举x,并将对应区间中的参赛人数求和,即可寻找参赛人数的最大值。
可以进行前缀和优化,或者滑动窗口。
Code:
#include <iostream>
using namespace std;
const int N = 1e6 + 9;
int a[N], sum = 0, ans, maxv = 0;
int main()
{int n;cin >> n;for (int i = 1; i <= n; i++) cin >> a[i % n];int s, f;cin >> s >> f;//在s-f区间内选取人数for (int i = s; i <= f; i++) sum += a[i % n];for (int i = 0; i < n; i++) {//由于模可能取到0,所以 将其改到n即可int l = (s - i + n) % n;int r = (f - i + n) % n;sum += a[l] - a[r];if (sum > maxv) maxv = sum, ans = i;}cout << ans + 1 << endl;return 0;
}
这篇关于CodeForces 939C Convenient For Everybody(前缀和+滑动窗口)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!