2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301

Distinct Values

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5312    Accepted Submission(s): 1823

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≠ajholds.
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

Sample Output

1 2

1 2 1 2

1 2 3 1 1

Source

2018 Multi-University Training Contest 1

题意概括:

需要求一个长度为 N 的序列,要求满足给出的 M 的子区间内不能出现重复的数,要求字典序最小。

解题思路:

一开始直接双指针模拟,但是果断WA,原因就是当前子区间不能的数不一定是连续的,需要记录前面不能使用的数。

后来用了一个标记数组记录所有不能用的数,TL,想想也知道,查询和维护需要时间复杂度太高。

那有什么可以快速维护这些当前可以使用的数,并且把已经使用过的数删除掉的呢?

C++ STL 里 的 set (可以说功能十分强大了,自动升序排序,可查询,删除)

那么如果知道了当前区间不能时候用什么数,能使用什么数,剩下的就是贪心策略了,尽可能地把需要满足条件子区间扩大(大的子区间满足了它所包含的小的子区间肯定也是满足的)。

用一个数组记录当前序列位置 x 所在的区间的最左值即可。

根据这个最左值 pre[x] 就可以判断当前以求的答案序列里 < pre[x] 的数都是可以用来填充当前子区间的。

AC code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<vector>
 6 #include<queue>
 7 #include<cmath>
 8 #include<set>
 9 #define INF 0x3f3f3f3f
10 #define LL long long
11 using namespace std;
12 const int MAXN = 1e5+10;
13 int num[MAXN];
14 int pre[MAXN];
15 int ans[MAXN];
16 set<int>ss;
17 int N, M;
18 int main()
19 {
20     int l, r;
21     int T_case;
22     scanf("%d", &T_case);
23     while(T_case--){
24         ss.clear();
25         scanf("%d %d", &N, &M);                                            //初始化
26         for(int i = 1; i <= N; i++){pre[i] = i; ss.insert(i);}
27         for(int i = 1; i <= M; i++){scanf("%d %d", &l, &r); pre[r] = min(pre[r], l);}
28         for(int i = N-1; i >= 1; i--) pre[i] = min(pre[i], pre[i+1]);
29         int p = 1;
30         for(int i = 1; i <= N; i++){
31             while(p < pre[i]){ss.insert(ans[p]);p++;}
32             ans[i] = *ss.begin();
33             ss.erase(ans[i]);
34         }
35
36         for(int i = 1; i <= N; i++){printf("%d", ans[i]);if(i < N) printf(" ");}
37         puts("");
38     }
39     return 0;
40 }

原文地址:https://www.cnblogs.com/ymzjj/p/10326676.html

时间: 2024-08-07 05:29:13

2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】的相关文章

2018 Multi-University Training Contest 1 - Distinct Values

set维护 预处理很巧妙,对于完全被大范围包含的小范围我们不用考虑,要处理的只有不完全重合的区间. 因为一个区间可能被下一个区间的一部分包含,所以我们所能选择的数是在变化的,用一个集合来维护,每次取最小值即可. 在读入区间范围的时候,可以用pre数组来存每个区间r对应的最大的区间长度的l,然后再反着用pre[i+1]更新pre[i],可以得到包含每个点的最大区间的l. 在填数字的时候考虑是否在下一个区间,用pl来维护两个区间不重叠的数,pl从l[i-1]移动到l[i+1]-1的过程中,经过的值我

2018 杭电多校1 - Distinct Values

题目: Problem Description Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements $$$a_i$$$ and  $$$a_j$$$ in the subarray $$$a_{l,r} (l ≤ i < j ≤ r )$$$ Input There are multiple test cases. The first

2018 Nowcoder Multi-University Training Contest 2

Practice Link A. run 题意: 白云每次可以移动\(1\)米或者\(k\)米,询问移动的米数在\([L, R]\)范围内的方案数有多少. 思路: \(dp[i][2]\)表示到第\(i\)米,是通过\(1\)米的方式过来的还是\(k\)米的方式过来的,递推即可. 代码: #include <bits/stdc++.h> using namespace std; #define N 100010 const int p = 1e9 + 7; int f[N][2], g[N];

2018 Nowcoder Multi-University Training Contest 1

Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdots, a_n\)中有多少个不同的数. 思路: 先分别离线求出\(a_1, \cdots a_i\)以及\(a_j, \cdots, a_n\)中有多少个不同的数. 再考虑有多少个数既在\([1, i]\)中也在\([j, n]\)中,再离线做一次. 考虑一个数第一次出现的时候,那么这个数下一次出现

2018 Nowcoder Multi-University Training Contest 5

Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ \begin{eqnarray*} gpa = \frac{\sum s_ic_i}{\sum s_i} \end{eqnarray*} \] 思路: 首先删去的课程越多,gpa肯定不会变得更差. 所以我们肯定是删去\(k\)门课程. 考虑二分答案,check的时候要满足: \[ \begin{eq

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 5943    Accepted Submission(s): 2004 Problem Description Before

杭电2018多校第一场(2018 Multi-University Training Contest 1) 1001.Maximum Multiple (HDU6298)-数学思维题(脑子是个好东西,可惜我没有)

暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.Maximum Multiple 题目意思就是给你一个n,找出来三个数x,y,z, 使得n=x+y+z,而且x,y,z都是n的因数,并且x*y*z为最大值,让你输出来x*y*z的最大值.如果没有满足条件的情况就输出-1. 由1=1/2+1/3+1/6=1/3+1/3+1/3=1/2+1/4+1/4,所

2018 Multi-University Training Contest 4

Problem D. Nothing is Impossible Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 504    Accepted Submission(s): 302 Problem Description m students, including Kazari, will take an exam tomorrow

HDU 6396 Swordsman --------2018 Multi-University Training Contest 7 (模拟+读入挂)

原题地址: 打怪升级 一开始有N个怪物:主角有K个能力:只有K个能力都击败怪物才能斩杀怪物并获得K个能力的增值:问最多能杀几个怪物: 做法: 用优先队列把怪物能力装进去:能力小放前面: 最重要的是数据量要用读入挂才能过:(读入挂太神奇了!!) Swordsman Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2049    Acce