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.s - i\leqslant x

2.f - i \geqslant x + 1

所以,参赛时区的区间为[s - x ,f - x - 1]

现在只需枚举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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值