1. 程式人生 > >HDU6301-2018ACM暑假多校聯合訓練1004-Distinct Values

HDU6301-2018ACM暑假多校聯合訓練1004-Distinct Values

一個 sum ram lds i++ lex cin case ever

題意是一個長度為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 }

HDU6301-2018ACM暑假多校聯合訓練1004-Distinct Values