Codeforces Round #642 (Div. 3) D

Codeforces Round #642 (Div. 3) D

优先队列的数据结构题
–>Link

Constructing the Array

  • 题意:给你一个全是0的数列长度,求以以下规则变换后的数列
    1. 优先取最长连续0子串,长度相等取左、
    2. 第n次操作时,把这个子串以给定方法取Mid,把数列的下标为Mid的值变成n。
  • 思路:我本来想,有没有贪心的方法去解,然而越想越像bfs,然后就bfs了。yysy,优先队列写得少,学了一些建设性的写法,码一下
#include<cassert>
#include<string>
#include<cmath>
#include<cstring>
#include<stack>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#pragma warning(disable:4996)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f
#define itn int
#define ll long long
#define mes(a,k) memset(a,k,sizeof(a))
//#define max(a,b) a>b?a:b
//#define min(a,b) a<b?a:b
//#define pb(a) push_back(a)
#define eps 1e-8

using namespace std;
const int N = 2e5 + 5;
using namespace std;
int T;
int ans[N];
struct node {
	int l, r;
	node(int a, int b) {//构造node,很方便
		l = a;
		r = b;
	}
	bool operator<(const node & a)const {//保证先走长度大的,再走左的
		if (r - l == a.r - a.l) return l > a.l;
		else return (r - l) < (a.r - a.l);
	}
};
void io() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
int getMid(int l, int r) {
	if ((r - l + 1) & 1)
		return (l + r) / 2;
	else
		return (l + r - 1) / 2;
}
int main() {
	io();
	cin >> T;
	while (T--) {
		int len;
		cin >> len;
		priority_queue<node> q;
		q.push(node(1, len));
		for (int i = 1; i <= len; i++) {
			node now = q.top();
			q.pop();
			int mid = getMid(now.l, now.r);
			ans[mid] = i;
			q.push(node(now.l, mid - 1));
			q.push(node(mid + 1, now.r));
		}
		while (!q.empty()) q.pop();
		for (itn i = 1; i <= len; i++) {
			cout << ans[i] << " ";
		}
		cout << endl;
		mes(ans, 0);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值