Distinct Values

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

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
 

题意:你需要构造一个长度为n的字典序最小的序列,要求如下

   1,有m个区间,在这些区间内,数字不得重复

   2,长度为n,不能有0出现

题解:既然要求字典序最小,那么我们那些没有被区间覆盖的地方一定是1

   区间覆盖的地方得特殊考虑,区间是否相交(重叠)等问题

   于是我们先把区间按照左端点排序,每次在区间内放尽可能小的就行

   格式要求行尾不能有多余的空格

代码如下

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <queue>
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn = 1e5+;
  9. struct node{
  10. int l,r;
  11. }a[maxn];
  12. bool cmp(node a,node b){
  13. if(a.l==b.l) return a.r<b.r;
  14. return a.l<b.l;
  15. }
  16. int ans[maxn];
  17. int vis[maxn];
  18. int main(){
  19. int T;
  20. scanf("%d",&T);
  21. while(T--){
  22. int n,m;
  23. memset(vis,,sizeof(vis));
  24. scanf("%d%d",&n,&m);
  25. for(int i=;i<=m;i++){
  26. scanf("%d%d",&a[i].l,&a[i].r);
  27. }
  28. sort(a+,a+m+,cmp);
  29. priority_queue<int,vector<int>,greater<int> >q;
  30. for(int i=; i<=n; i++){
  31. ans[i]=;vis[i]=;
  32. q.push(i);
  33. }
  34. int l=a[].l,r=a[].l;
  35. for(int i=;i<=m;i++){
  36. for(;l<a[i].l;l++){
  37. if(vis[l]) q.push(ans[l]);
  38. }
  39. for(;r<=a[i].r;r++){
  40. if(r>=a[i].l){
  41. ans[r]=q.top();q.pop();
  42. vis[r]=;
  43. }
  44. }
  45. }
  46. for(int i=; i<=n; i++) {
  47. printf("%d",ans[i]);
  48. if(i==n) printf("\n");
  49. else printf(" ");
  50. }
  51. }
  52. }

HDU 多校对抗赛 D Distinct Values的更多相关文章

  1. HDU 多校对抗赛第二场 1004 Game

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDU 多校对抗赛第二场 1010 Swaps and Inversions

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU 多校对抗赛 C Triangle Partition

    Triangle Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Oth ...

  5. HDU 多校对抗赛 A Maximum Multiple

    Maximum Multiple Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 多校对抗赛 J Time Zone

    Time Zone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. hdu多校1004 Distinct Values

    Distinct Values Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): ...

  8. hdu 6301 Distinct Values (思维+set)

    hdu 6301 Distinct Values 题目传送门 题意: 给你m个区间,让你求出一个长度为n的区间且满足在这些区间的数不重复, 并且要求字典序最小 思路: 如果我们已经求出这个序列了,你会 ...

  9. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. fastDFS 上传 java源码

    要想搭建fastDFS网上有相近的文章: 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试 首先下载fastd ...

  2. 关于C++类模板无法解析的问题

    自己写了一个C++模板类,可是在vs2012中死活显示无法解析它的成员函数. 开始怎么也想不通,因为我是按照普通方式布置的:头文件放声明,在同名源文件中放实现,并包含其头文件. 后来百度了一下才发现, ...

  3. Spring+quartz cron表达式(cron手册官方)完美理解

    ------------------------------------- 15 17/1 14/3 * * ? 从每小时的17分15秒开始 每分钟的15秒执行一次14:17:15 ...14:59: ...

  4. netty源码分析系列文章

    netty源码分析系列文章 nettynetty源码阅读netty源码分析  想在年终之际将对netty研究的笔记记录下来,先看netty3,然后有时间了再写netty4的,希望对大家有所帮助,这个是 ...

  5. Vue学习(四):条件渲染

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Python-学习-小例子练习

    网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...

  7. centos7用yum搭建LAMP环境

    用yum快速搭建LAMP平台 实验环境: [root@nmserver- html]# cat /etc/redhat-release CentOS release (AltArch) [root@n ...

  8. 《python机器学习—预测分析核心算法》:理解数据

    参见原书2.1-2.2节 新数据集就像一个包装好的礼物,它充满了承诺和希望! 但是直到你打开前,它都保持神秘! 一.基础问题的架构.术语,机器学习数据集的特性 通常,行代表实例,列代表属性特征 属性, ...

  9. 1003 Emergency (25 分)(求最短路径)

    给出N个城市,m条无向边.每个城市中都有一定数目的救援小组,所有边的边权已知.现在给出起点和终点,求从起点到终点的最短路径条数及最短经上的救缓小组数目只和.如果有多条最短路径,则输出数目只和最大的 D ...

  10. 【CodeForces】9B-Running Student

    目录 Question Description Input Output Solution 解法1 Question Description 小明在公交车始发站上车,他应该在哪个站点下车才能最快到达学 ...