题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小
同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列
贪心是先采用pre数组来确定哪些区间不能重复,再通过记录从set弹出答案的位置来计算的
Problem Description
Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (l≤i<j≤r), ai≠aj holds.
Chiaki would like to find a lexicographically minimal array which meets the facts.
Chiaki would like to find a lexicographically minimal array which meets the facts.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m (1≤n,m≤105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1≤li≤ri≤n).
It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.
Output
For each test case, output n integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines.
Sample Input
3
2 1
1 2
4 2
1 2
3 4
5 2
1 3
2 4
2 1
1 2
4 2
1 2
3 4
5 2
1 3
2 4
Sample Output
1 2
1 2 1 2
1 2 3 1 1
1 2 1 2
1 2 3 1 1
#include <iostream>
#include <set>
#include <algorithm> using namespace std; int pre[];
int ret[]; int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t; while (t--)
{
set<int> s;
int m, n;
cin >> n >> m;
for (int i = ; i <= n; i++)
{
pre[i] = i;
s.insert(i);
} for (int i = ; i <= m; i++)
{
int l, r;
cin >> l >> r;
pre[r] = min(pre[r], l);
} for (int i = n - ; i > ; i--)
pre[i] = min(pre[i], pre[i + ]); int pos = ;
for (int i = ; i <= n; i++)
{
while (pre[i] > pos)
{
s.insert(ret[pos]);
pos++;
}
ret[i] = *s.begin();
s.erase(ret[i]);
} for (int i = ; i <= n; i++)
{
cout << ret[i];
if (i != n)
cout << " ";
}
cout << endl;
} return ;
}