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;
}