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

Posted qq965921539

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

 








以上是关于HDU6301-2018ACM暑假多校联合训练1004-Distinct Values的主要内容,如果未能解决你的问题,请参考以下文章

HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple

HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

暑假N天乐比赛篇 —— 2019杭电暑期多校训练营(第五场)

2015暑假多校联合---Mahjong tree(树上DP 深搜)

2015暑假多校联合---Expression(区间DP)

2015暑假多校联合---CRB and His Birthday(01背包)