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. ArcGIS 切片与矢量图图层顺序问题

    在项目中有个需求:根据图层索引添加图层 看到这个需求一下子想到 map.addLayer(layer,index?) 接口 但是问题出现了,我切片图加载顺序在矢量图之后就不行! map = new M ...

  2. 20191107-7 beta week 2/2 Scrum立会报告+燃尽图 06

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9959 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 ...

  3. 迁移桌面程序到MS Store(13)——动态检查Win10 API是否可用

    假设我们现有一个WPF程序,需要支持1903以前的Windows 10版本.同时在1903以后的版本上,额外多出一个Ink的功能.那么我们就可以通过ApiInformation.IsApiContra ...

  4. ASP.NET Core 中的 ObjectPool 对象重用(一)

    前言 对象池是一种设计模式,一个对象池包含一组已经初始化过且可以使用的对象,而可以在有需求时创建和销毁对象.池的对象可以从池中取得对象,对其进行操作处理,并在不需要时归还给池子而非直接销毁他,他是一种 ...

  5. 【数据结构】之栈(Java语言描述)

    在前面的[这篇文章]中,我简单介绍了栈这种数据结构的操作功能,并使用C语言对其进行了代码的编写. Java的JDK中默认为我们提供了栈这种数据结构的API—— Stack . Java中的Stack类 ...

  6. jitter()函数的使用

    jitter()函数:对数值向量添加一个小的噪音量. jitter(x,factor=1,amount=NULL) ·x:数值变量,需要加入噪音的数值向量: ·factor:数值型: ·amount: ...

  7. 手机端web(iPad)页面自适应js

    有关编写手机页面(ipad页面)自适应的方法有很多,比如:bootstrap,rem等等.下面分享给大家一个js控制viewPort视区自适应缩放的方法(我给它命名为phone.js): 将phone ...

  8. Python数据分析I

    Python数据分析概述 数据分析的含义与目标 统计分析方法 提取有用信息 研究.概括.总结 Python与数据分析 Python: Guido Van Rossum Christmas Holida ...

  9. RestSharp Simple REST and HTTP API Client for .NET

    var client = new RestClient("http://example.com"); // client.Authenticator = new HttpBasic ...

  10. Spring IOC容器装配Bean_基于XML配置方式

    开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...