Distinct Values

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

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的自然数序列,使得m个给定区间li...ri的数字互不相同。
 
贪心。
首先处理m个给定的区间。用ri数组存储1...n对应的最大右边界,使区间数字互不相同。更新是这样的:ri[i]=max(r,ri[i])。
然后遍历ri数组,依次处理每个区间即可。当前区间包含待处理区间时,可以直接跳过。技巧性在于要利用一个值越小优先级越高的1...n的优先队列。不断把其中的数拿出来有放回以实现字典序最小。
 
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath> using namespace std; const int maxn=; int ri[maxn+];
int arr[maxn+]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m); for(int i=;i<=n;i++)
ri[i]=i;
for(int i=,a,b;i<m;i++)
{
scanf("%d%d",&a,&b);
ri[a]=max(ri[a],b);
} priority_queue<int,vector<int>,greater<int> > q;
for(int i=;i<=n;i++)
q.push(i); q.pop();arr[]=;
for(int i=,a=,b=;i<=n;i++)
{
if(ri[i]<=b) continue;
for(int j=a;j<i;j++)
q.push(arr[j]);
for(int j=b+;j<=ri[i];j++)
{
arr[j]=q.top();
q.pop();
}
a=i;b=ri[i];
} for(int i=;i<=n;i++)
{
if(i==)
printf("%d",arr[i]);
else
printf(" %d",arr[i]);
}
printf("\n");
}
return ;
}

hdu 6301 Distinct Values (贪心)的更多相关文章

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

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

  2. 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 ...

  3. hdu 6301 Distinct Values(贪心)题解

    题意:长为n的串,给你m个区间,这些区间内元素不重复,问这样的串字典序最小为? 思路:用set保存当前能插入的元素,这样就能直接插入最小元素了.对操作按l排序,因为排过的不用排,所以两个指针L,R是一 ...

  4. HDU 6301 Distinct Values

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6301 多校contest1 题目大意是有一个长度为N的数组,给出M个"事实",每个 ...

  5. hdu 6301 Distinct Values (双指针,水题)

    大意: 给定m个区间, 求构造一个长n且字典序最小的序列, 使得每个区间内的数各不相同 求出每个位置为左端点时向右延伸最大距离, 然后双指针, 每次从set中取最小 #include <iost ...

  6. HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)

    HDU6301.Distinct Values 这个题就是给你区间要求区间内的数都不相同,然后要求是字典序最小,直接贪心走一遍,但是自己写的时候,思路没有错,初始化写挫了... 将区间按左端点小的排序 ...

  7. Distinct Values(贪心)

    问题 D: Distinct Values 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 5[提交] [状态] [讨论版] [命题人:admin] 题目描述 Chiaki ...

  8. 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/Ot ...

  9. hdu多校1004 Distinct Values

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

随机推荐

  1. Netflix 开源 Polynote:对标 Jupyter,一个笔记本运行多种语言

    谈到数据科学领域的开发工具,Jupyter 无疑是非常知名的一种.它具有灵活高效的特点,非常适合进行开发.调试.分享和教学.近日,Netflix(奈飞)居然也玩起了跨界,他们开源了一个名为 Polyn ...

  2. python CGI编程---Apache服务安装(2)

    一.下载Apache 下载地址:https://www.apachehaus.com/cgi-bin/download.plx 我这里下载第一个,我电脑是window的64位. 下载完成后,解压到 我 ...

  3. Linux下为知笔记和蚂蚁笔记测评,推荐蚂蚁笔记!(非广告)

    本人由于学习Linux,需要一款可以在Linux平台下可以运行的一款软件,了解到为知笔记之笔记(下文以W代替)和蚂蚁笔记(下文以M代替)比较出名,由于某云和某象笔记在linux平台下没有对应的软件,所 ...

  4. PyQt5的安装及测试(pycharm)

    参考链接:https://www.cnblogs.com/pywjh/articles/9835931.html     https://blog.csdn.net/SeekAndFindYou/ar ...

  5. 如何平滑优雅地在Rancher 2.x中升级cert-manager?

    作者: Nassos Michas丨European Dynamics SA, CTO 如果你正在使用由Rancher提供的Helm Chart在Rancher管理的Kubernetes集群中安装ce ...

  6. 【Android - 自定义View】之View的layout过程解析

    layout(布局)的作用是ViewGroup用来确定子元素的位置,在这个过程中会用到两个核心方法: layout() 和 onLayout() .layout()方法用来确定View本身的位置,on ...

  7. 高并发编程-AQS深入解析

    要点解说 AbstractQueuedSynchronizer简称AQS,它是java.util.concurrent包下CountDownLatch/FutureTask/ReentrantLock ...

  8. IDEA启动tomcat报java.net.SocketExceptionsocket closed

    IDEA启动tomcat报java.net.SocketException:socket closed.如图所示   解决方法:打开任务管理器,检查有没有java.exe进程. 关闭了重新启动就好了 ...

  9. elk的搭建

    一:准备工作 1.准备一台虚拟机 192.168.175.222      elk-node2 2.关闭防火墙以及selinux 命令:systemctl stop firewalld       # ...

  10. SDK版本管理

    在编写API时,有些API被废弃.如何在使用者调用该API时就报出已经被废弃呢? 方法如下: 1.在OC中 在@interface里将要废弃的方法引用后边加上 __attribute__((depre ...