Codeforces Round #642 (Div. 3) D.Constructing the Array

Codeforces Round #642 (Div. 3)-1353

D.Constructing the Array

问题分析:

模拟一个0数列,用优先队列 然后把按照两个规则进行出列 连续0长度长的优先级高,同长度的开始位置考前优先 具体看大佬代码

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
#define mem(a,b) memset((a),(b),sizeof(a));
#define map map mmp;
const ll inf=0x3f3f3f3f;//1061109567,2*未超int,allinf=mem(a,0x3f,sizeof(a));
const int N=2e5+10;
int a[N];
struct node {
     
	int l, r, len;
	node(int a, int b, int c) {
     
		l = a;
		r = b;
		len = c;
	}
	//优先队列从大到小排
	//返回值为真意味着在队列排序中 this
	bool operator < (const node& a) const {
     
		if (a.len == len) return l > a.l;
		//如果长度相同而a的l较小(即靠近左边),则a>this(a更优先)
		return len < a.len;
		//否则如果a更长,则a>this(a更优先)
	}
};
int main(){
     
       // #define io
#ifdef io
        freopen("in.txt","r",stdin);
#endif
ios::sync_with_stdio(0);
int t;
cin>>t;
while(t--){
     
int n;
	cin >> n;
	for (int i = 1; i <= n; i++) a[i] = 0;
	priority_queue<node> q;
	q.push(node(1, n, n));
	for (int i = 1; i <= n; i++) {
     
		node u = q.top();
		q.pop();
		int num = (u.len % 2) ? (u.l + u.r) / 2 : (u.l + u.r - 1) / 2;
		a[num] = i;
		q.push(node(u.l, num - 1, num - u.l));
		q.push(node(num + 1, u.r, u.r - num));
	}
	for (int i = 1; i <= n; i++) cout << a[i] << " ";
	cout << endl;
}
return 0;
}

你可能感兴趣的:(CodeForces)