【cf】Constructing the Array(set类与pair灵活结合的题)(Codeforces Round #642 (Div. 3) D题)

链接:https://codeforces.ml/contest/1353/problem/D
题解来源:https://codeforces.ml/blog/entry/77373

题目:

You are given an array a of length n consisting of zeros. You perform n actions with this array: during the i-th action, the following sequence of operations appears:

·Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one;
·Let this segment be [l;r]. If r−l+1 is odd (not divisible by 2) then assign (set) a[l+r2]:=i (where i is the number of the current action), otherwise (if r−l+1 is even) assign (set) a[l+r−12]:=i.
Consider the array a of length 5 (initially a=[0,0,0,0,0]). Then it changes as follows:

·Firstly, we choose the segment [1;5] and assign a[3]:=1, so a becomes [0,0,1,0,0];
·then we choose the segment [1;2] and assign a[1]:=2, so a becomes [2,0,1,0,0];
·then we choose the segment [4;5] and ·assign a[4]:=3, so a becomes [2,0,1,3,0];
·then we choose the segment [2;2] and assign a[2]:=4, so a ·becomes [2,4,1,3,0];
·and at last we choose the segment [5;5] and assign a[5]:=5, so a becomes [2,4,1,3,5].

Your task is to find the array a of length n after performing all n actions. Note that the answer exists and unique.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (1≤n≤2⋅105) — the length of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output

For each test case, print the answer — the array a of length n after performing n actions described in the problem statement. Note that the answer exists and unique.

题意:

n个数一开始是都是0,每次选取最长连续零段,赋予中间值当前查询次数值(1~n)。

题解:

运用自定义排序set存储所有连续零段。

代码:

#include <bits/stdc++.h>

using namespace std;

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		set<pair<int, int>, cmp> segs;
		segs.insert({0, n - 1});
		vector<int> a(n);
		for (int i = 1; i <= n; ++i) {
			pair<int, int> cur = *segs.begin();
			segs.erase(segs.begin());
			int id = (cur.first + cur.second) / 2;
			a[id] = i;
			if (cur.first < id) segs.insert({cur.first, id - 1});
			if (id < cur.second) segs.insert({id + 1, cur.second});
		}
		for (auto it : a) cout << it << " ";
		cout << endl;
	}
	
	return 0;
}

初识:

pair的使用。
set的自定义排序。
for(auto it:a) 等价 for(auto it=a.begin();it!=a.end();++it)
(但是这个方法在我的cb上不经过预处理直接编译会报错)
for(auto &it:a) 的区别在于是否能对原数据进行修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值