CF1237D Balanced Playlist(st表)

题目传送

1.先分析-1的情况:最小值的两倍大于等于最大值,就可以无限循环
2.如果直接暴力的话,肯定超时,我还试了一下。。。
3.考虑最坏的情况,最大值在1,最小值在n,从i+1开始播放,假设中间没有满足的情况,需要循环三圈,所以数组开三倍。
4.从i开始播放,假设到j停止,从i+1播放,就不可能在j之前停,所以直接从j开始继续遍历就好,此时最大值更新为i+1到j之间的最大值,提前用st表处理。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lowbit(x) (x & -x)
#define lrt nl, nr, rt << 1
#define rrt nl, nr, rt << 1 | 1
const ll Inf = 1e18;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
	return f * x;
}

int an[maxn * 3];
int st[maxn * 3][35];

int getmaxx(int l, int r)
{
	int k = log2(r - l + 1);
	return max(st[l][k], st[r - (1 << k) + 1][k]);
}

int main(void)
{
	int n;
	n = read();
	int maxx = 0, minx = inf;
	for (int i = 1; i <= n; i++) {
		an[i] = an[i + n] = an[i + 2 * n] = read();
		maxx = max(maxx, an[i]);
		minx = min(minx, an[i]);
		st[i][0] = st[i + n][0] = st[i + 2 * n][0] = an[i];
	}
	if (maxx <= 2 * minx) {
		for (int i = 1; i <= n; i++) printf("-1 ");
		return 0;
	}
	for (int j = 1; j <= 30; j++) {
		for (int i = 1; i + (1 << j) - 1 <= 3 * n; i++) {
			st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
		}
	}
	maxx = an[1];
	int pos = 1;
	for (int i = 1; i <= n; i++) {
		while (an[pos] * 2 >= maxx) {
			maxx = max(maxx, an[pos]);
			pos++;
		}
		printf("%d ", pos - i);
		maxx = getmaxx(i + 1, pos);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值