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. PHP 的面向对象 与 类

    面向对象 == OO 学习面向对象 == XXOO;[学习使我快乐!] <!--附一个demo类的实例化--> http://note.youdao.com/noteshare?id=38 ...

  2. mac如何开启两个vmware虚拟机

    转载链接:https://blog.csdn.net/aifore/article/details/87833088

  3. 性能测试:深入理解线程数,并发量,TPS,看这一篇就够了

    并发数,线程数,吞吐量,每秒事务数(TPS)都是性能测试领域非常关键的数据和指标. 那么他们之间究竟是怎样的一个对应关系和内在联系? 测试时,我们经常容易将线程数等同于表述为并发数,这一表述正确吗? ...

  4. JAVA——汉诺塔

    大家还记得某年春晚小品那个把大象放冰箱需要几步吗? 今天,我准备写的是汉诺塔,有三个魔法石柱,分别:诚实.勇敢.正直.其中有一个石柱上从大到小,从地向上依次排放着四个魔法圆环,需要将那四个魔法圆环分别 ...

  5. 【2018寒假集训Day 7】【最短路径】三种算法的模板

    Luogu单源最短路径模版题 dijkstra #include<cstdio> #include<vector> using namespace std; const int ...

  6. Java面向基础概述和三大特性

    Java 是面向对象的高级编程语言,类和对象是 Java 程序的构成核心.围绕着 Java 类和 Java 对象,有三大基本特性:封装是 Java 类的编写规范.继承是类与类之间联系的一种形式.而多态 ...

  7. 【JavaEE】之MyBatis输出映射

    MyBatis中的输出映射有两种:resultType和resultMap. 1.resultType 使用resultType进行结果映射时,只有当查询结果中有至少一列的名称和resultType指 ...

  8. Java中我常用到的十二个最基本的快捷键

    自己在Java中经常用到的快捷键先记下来方便日后学习之用: 1. Ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按 ...

  9. 一统江湖的大前端(8)- velocity.js 运动的姿势(上)

    [摘要] 介绍CSS动画和JS动画的基本特点,以及轻量级动画库velocity.js的基本用法. 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园 ...

  10. 使用Python进行防病毒免杀

    很多渗透工具都提供了权限维持的能力,如Metasploit.Empire和Cobalt Strike,但是都会被防病毒软件检测到这种恶意行为.在探讨一个权限维持技巧的时候,似乎越来越多的人关注的是,这 ...