任意门: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
 

题意概括:

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

解题思路:

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

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

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

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

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

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

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

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
int num[MAXN];
int pre[MAXN];
int ans[MAXN];
set<int>ss;
int N, M;
int main()
{
int l, r;
int T_case;
scanf("%d", &T_case);
while(T_case--){
ss.clear();
scanf("%d %d", &N, &M); //初始化
for(int i = ; i <= N; i++){pre[i] = i; ss.insert(i);}
for(int i = ; i <= M; i++){scanf("%d %d", &l, &r); pre[r] = min(pre[r], l);}
for(int i = N-; i >= ; i--) pre[i] = min(pre[i], pre[i+]);
int p = ;
for(int i = ; i <= N; i++){
while(p < pre[i]){ss.insert(ans[p]);p++;}
ans[i] = *ss.begin();
ss.erase(ans[i]);
} for(int i = ; i <= N; i++){printf("%d", ans[i]);if(i < N) printf(" ");}
puts("");
}
return ;
}

最新文章

  1. WPF这样的界面应该如何编写呢?
  2. 细数那些我们都习惯了的Java谣言
  3. [手机取证] 绕过屏幕锁定启用调试模式-For Android 4.4.2
  4. git版本管理策略及相关技巧(A)
  5. 小甲鱼python视频弟十二讲(关于字符串的方法及注释下)
  6. IIS增加并发数
  7. transform.localPosition操作时的一些注意事项
  8. poi2012完成
  9. Gradle – Spring 4 MVC Hello World Example – Annotation
  10. HTML——&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  11. gitcafe 使用hexo搭建博客
  12. [置顶] mybatis批量新增系列之有主键的表的批量新增
  13. iOS 获取高速随机路径sandbox目录
  14. KKlist团队目录
  15. Linux学习之CentOS(十四)----磁盘管理之 硬连接与软件连接(转)
  16. [OpenCV] GpuMat and Mat, compare cvtColor perforemence
  17. SiteCore Experience Analytics-路径分析地图
  18. ionic 前端接收到后台推送来的消息后,显示在手机的通知栏上
  19. Vue.js 2.0生命周期
  20. 如何使用Javascript XSLT 处理XML文件(支持Firefox)

热门文章

  1. VS快捷键设置无效
  2. show_space
  3. php mktime()函数
  4. spring mvc如何优雅的使用fastjson
  5. OC与JS交互之JavaScriptCore
  6. 缓存与DB数据一致性问题解决的几个思路
  7. MySQL查询(未完结)
  8. img底部空白以及多余的空白文本节点解决方案
  9. Python-并发编程(协程)
  10. C语言实现整数数组的逆置算法