CF1353 D. Constructing the Array

Question

給你一串長度爲nn,值爲00的數組,每次找到最長00子串,將其中間(奇數中間L+R2\frac{L+R}{2},若爲偶數則選L+R12\frac{L+R-1}{2})

Solution

優先隊列模擬
利用優先隊列保存[L,R][L,R],每次保存去掉其中間之後的滿足題意的子區間[L,mid1][L,mid-1],[mid+1,R][mid+1,R]

優先隊列是用運算符<<,但是排序的順序和sort是相反的,所以裏面的符號要和sort時用的寫起來相反即可。

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;
const ll  N = 2e5 + 5;

struct node{
	int l,r;
	bool operator < (const node &T) const{
		if(r-l==T.r-T.l) return r>T.r;
		return r-l<T.r-T.l;
	}
};

void solve(){
	int n;cin>>n;
	priority_queue<node>q;

	q.push({1,n});
	int cnt=0;
	vector<int> a(n+1);
	while(!q.empty()){
		cnt++;
		int L=q.top().l;
		int R=q.top().r;
		q.pop();
		int mid;
		if((R-L+1)&1)
			mid=(L+R)/2;
		else
			mid=(L+R-1)/2;
		a[mid]=cnt;
		if(L<=mid-1 && L>=1) q.push({L,mid-1});
		if(R>=mid+1 && R<=n) q.push({mid+1,R});
	}
	for(int i=1;i<=n;i++){
		cout<<a[i]<<" \n"[i==n];
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;cin>>T;
	while(T--){
		solve();
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章