Codeforces 1506E 构造

1506E
题意:
两个数组a,b其中bi是max(a1~ai);,求字典序最大和最小的数组a;
思路:
数组b一定是逐渐上升的,假如两个相邻的元素相同那么第二个元素一定是小于前一个的;
这个题可以使用优先队列来做,建立大根堆和小根堆,最大就插入大根堆堆顶,最小就插入小根堆堆顶;

#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int N = 200010;
int a[N], b[N];
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		map<int, int> mp;
		for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]++;
		priority_queue<int> q_max;
		priority_queue<int, vector<int>, greater<int>> q_min;
		int x = a[1];
		cout << x << " ";
		vector<int>v;
		v.push_back(x);
		for (int i = 1; i < x; i++) q_max.push(i), q_min.push(i);//找到小于x的所有数,插入堆中;
		for (int i = 2; i <= n; i++) {
			if (x == a[i]) {//输出对应的堆顶元素;
				cout << q_min.top() << " ";
				q_min.pop();
				v.push_back(q_max.top());
				q_max.pop();

			} else {
				for (int j = x + 1; j < a[i]; j++) q_max.push(j), q_min.push(j);//假如对应的堆顶元素;
				x = a[i];
				v.push_back(x);
				cout << x << " ";
			}
		}
		cout << endl;
		for (auto i : v) cout << i << " ";
		cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值