Balanced Playlist CodeForces - 1237D

Balanced Playlist CodeForces - 1237D

题意:给出n首歌的循环播放列表。每首歌有一个欢乐值。播放到一首歌,这首歌的欢乐值的两倍小于遇到过的最大值,则停止。求出任意一首歌起始播放时,最多能播放多少歌。
思路:在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <vector>
#include <set>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
using namespace std;
const int maxn = 300009;
int q[maxn], a[maxn], inq[maxn], ans[maxn];
int main()
{
	int n; cin >> n;
	int mx = 0, mi = inf; 分别是求最大值和最小值
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];输入音乐值
		a[i + n] = a[i];
		a[i+2*n] = a[i];
		mx = max(a[i], mx);
		mi = min(a[i], mi);
	}
	if (mx <= 2 * mi)如果最大值小于2*最小值,这说明可以一直播放
	{
		for (int i = 1; i <= n; i++)
			cout << "-1 ";
		return 0;
	}
	int h = 0, t = 1;
	for (int i = 1; i <= 3 * n; i++)
	{
		while (h <= t && q[t] < a[i])
		{
			t--;
		}
		q[++t] = a[i];
		inq[t] = i;
		while (q[h] > a[i] * 2)
		{
			ans[inq[h]] = i - inq[h];
			h++;
		}
	}
	for (int i = 3 * n; i >= 1; i--)
	{
		if (ans[i] == 0)
			ans[i] = ans[i + 1] + 1;
	}
	for (int i = 1; i <= n; i++)
		cout << ans[i] << " ";
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值