HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

题意是一个长度为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 (li<jr), aiaj holds.
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 (1n,m105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1lirin).

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
 
Sample Output
1 2 1 2 1 2 1 2 3 1 1
 
 1 #include <iostream>
 2 #include <set>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 int pre[100010];
 8 int ret[100010];
 9 
10 int main()
11 {
12     ios::sync_with_stdio(false);
13     int t;
14     cin >> t;
15 
16     while (t--)
17     {
18         set<int> s;
19         int m, n;
20         cin >> n >> m;
21         for (int i = 1; i <= n; i++)
22         {
23             pre[i] = i;
24             s.insert(i);
25         }
26 
27         for (int i = 1; i <= m; i++)
28         {
29             int l, r;
30             cin >> l >> r;
31             pre[r] = min(pre[r], l);
32         }
33 
34         for (int i = n - 1; i > 0; i--)
35             pre[i] = min(pre[i], pre[i + 1]);
36 
37         int pos = 1;
38         for (int i = 1; i <= n; i++)
39         {
40             while (pre[i] > pos)
41             {
42                 s.insert(ret[pos]);
43                 pos++;
44             }
45             ret[i] = *s.begin();
46             s.erase(ret[i]);
47         }
48 
49         for (int i = 1; i <= n; i++)
50         {
51             cout << ret[i];
52             if (i != n)
53                 cout << " ";
54         }
55         cout << endl;
56     }
57 
58     return 0;
59 }
 

猜你喜欢

转载自www.cnblogs.com/qq965921539/p/9360632.html
今日推荐