Codeforces - D. Balanced Playlist

Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of n tracks numbered from 1 to n. The playlist is automatic and cyclic: whenever track i finishes playing, track i+1 starts playing automatically; after track n goes track 1.

For each track i, you have estimated its coolness ai. The higher ai is, the cooler track i is.

Every morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness x of already played tracks. Once you hear that a track with coolness strictly less than x2 (no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood.

For each track i, find out how many tracks you will listen to before turning off the music if you start your morning with track i, or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted.

Input
The first line contains a single integer n (2≤n≤105), denoting the number of tracks in the playlist.

The second line contains n integers a1,a2,…,an (1≤ai≤109), denoting coolnesses of the tracks.

Output
Output n integers c1,c2,…,cn, where ci is either the number of tracks you will listen to if you start listening from track i or −1 if you will be listening to music indefinitely.

Examples
inputCopy
4
11 5 2 7
outputCopy
1 1 3 2
inputCopy
4
3 2 5 3
outputCopy
5 4 3 6
inputCopy
3
4 3 6
outputCopy
-1 -1 -1
Note
In the first example, here is what will happen if you start with…

track 1: listen to track 1, stop as a2<a12.
track 2: listen to track 2, stop as a3<a22.
track 3: listen to track 3, listen to track 4, listen to track 1, stop as a2<max(a3,a4,a1)2.
track 4: listen to track 4, listen to track 1, stop as a2<max(a4,a1)2.
In the second example, if you start with track 4, you will listen to track 4, listen to track 1, listen to track 2, listen to track 3, listen to track 4 again, listen to track 1 again, and stop as a2<max(a4,a1,a2,a3,a4,a1)2. Note that both track 1 and track 4 are counted twice towards the result.


直观的想法就是一直维护最大值,并且支持删除某个值。

所以我们可以考虑使用权值线段树来维护最大值。并且需要离散化。

因为区间的循环的,所以我们可以把区间放缩成三倍,因为当3次到同一个地方就代表是-1。然后我们一直用一个指针向右边走,知道不满足条件或者第三次到达这个点就停下来。

比赛的时候线段树没开4倍,一直RE好气!!(其实用平衡树维护即可,然后我们可以想到set)


权值线段树代码:

AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,a[N<<3],cnt,b[N],m,l,r,res[N];
map<int,int> mp;
struct node{
	int l,r,sum;
}t[N<<2]; 
inline void push_up(int p){
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}
void build(int p,int l,int r){
	t[p].l=l; t[p].r=r;
	if(l==r)	return; int mid=l+r>>1;
	build(p<<1,l,mid);	build(p<<1|1,mid+1,r);
}
void change(int p,int x,int v){
	if(t[p].l==t[p].r){
		t[p].sum+=v; return ;
	}
	int mid=t[p].l+t[p].r>>1;
	if(x<=mid)	change(p<<1,x,v);
	else	change(p<<1|1,x,v);
	push_up(p);
}
int ask(int p){
	if(t[p].l==t[p].r)	return t[p].l;
	if(t[p<<1|1].sum>0)	return ask(p<<1|1);
	else	return ask(p<<1);
}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)	scanf("%d",&a[i]),b[i]=a[i];
	sort(b+1,b+1+n);	m=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<=m;i++)	mp[b[i]]=++cnt;
	build(1,1,n);
	for(int i=1;i<=n;i++)	a[i+n]=a[i+2*n]=a[i+3*n]=a[i]; l=1;
	for(int i=1;i<=n;i++){
		if(i==l)	change(1,mp[a[i]],1); if(l==i)	l++;
		while((l-i)<2*n&&b[ask(1)]<=a[l]*2){
			change(1,mp[a[l]],1);	l++;
		}
		if((l-i)==2*n)	res[i]=-1;
		else	res[i]=l-i;
		change(1,mp[a[i]],-1);
	}
	for(int i=1;i<=n;i++)	cout<<res[i]<<' ';puts("");
	return 0;
}

AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,a[N<<2],res[N],l=1;
multiset<int> st;
inline int ask(){return *(--st.end());}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)	scanf("%d",&a[i]),a[i+n]=a[i+2*n]=a[i];
	for(int i=1;i<=n;i++){
		if(i==l)	st.insert(a[i]),l++;
		while(l-i<2*n&&ask()<=a[l]*2)	st.insert(a[l]),l++;
		res[i]=((l-i==2*n)?-1:l-i);
		st.erase(st.find(a[i]));
	}
	for(int i=1;i<=n;i++)	printf("%d ",res[i]);puts("");
	return 0;
}
全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务