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. Java流程控制之(三)嵌套

    目录 嵌套循环 for循环嵌套 while循环嵌套 总结 之前谈到各种循环结构,有for循环啊,有while循环啊,可以完成不断重复的动作,相当方便.那么如果好多个循环结合再一次,又是如何实现效果的呢 ...

  2. 2019-10-12,html+php+mysql简单留言板,作业

    php+mysql简易留言板,实现注册,登录,注销,查看留言,删除留言 1,index.html登录页面 代码: <!doctype html> <html> <head ...

  3. 数位dp介绍

    不了解dp的可以先看一下dp 数位dp含义: 数位:一个数有个位,十位,百位,千位等等,数的每一位都是数位. 数位dp归为计数dp,是在数位上进行操作的dp. 数位dp的实质是一种快速枚举的方式,它满 ...

  4. 用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo

    简介 完整代码 https://github.com/PuZhiweizuishuai/SpringSecurity-JWT-Vue-Deom 运行展示 后端 主要展示 Spring Security ...

  5. 2019-2020-4 20199317《Linux内核原理与分析》第四周作业

    第3章  MenuOS的构造 1  Linux内核源代码简介        计算机的“3大法宝”:存储程序计算机.函数调用堆栈和中断. 操作系统的“两把宝剑”:一把是中断上下文的切换——保存现场和恢复 ...

  6. input监听

    <h1> 实时监测input中值的变化 </h1> <input type="text" id="username" autoco ...

  7. Hadoop原生搭建

    版本:(centos7.6) 在开始搭建平台前我已经预装了MySQL ps:MySQL创建用户并授权: grant all privileges on *.* to ' with grant opti ...

  8. Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)

    View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇  ...

  9. 《Windows内核安全与驱动开发》4.1 文件操作

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发>4.1 文件操作 从 C:\a.txt 中读取一部分内容并利用 DbgPrin ...

  10. MySQL数据库开发的36条原则【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...