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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章