Distinct Values

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

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
 
Source
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int N=1e5+;
int num[N],n,m,pre[N];
int t;
set<int>s;
int main()
{
scanf("%d",&t);
int l,r;
//pre[i]: 在i位置放数时必须要考虑到的最左的位置
while(t--)
{ s.clear();
scanf("%d%d",&n,&m);
memset(num,,sizeof(num));
for(int i=;i<=n;i++)
{ pre[i]=i;
s.insert(i);
}
for(int i=;i<m;i++)
{
scanf("%d%d",&l,&r);
pre[r]=min(pre[r],l);
}
for(int i=n-;i>=;i--){//pre[i]<=pre[i+1]
pre[i]=min(pre[i],pre[i+]);//如:pre[i+1]=1,pre[i]=2,那么pre[i]一定=1
}
int begin=;
for(int i=;i<=n;i++){
while(begin<pre[i]){//可以和begin重复
s.insert(num[begin]);//插入新的
begin++;//同时位置不断的右移
}
num[i]=*s.begin();//最小的(s里面都是满足题意的)
s.erase(num[i]);//避免重复。
}
for(int i=;i<=n;i++){
printf("%d%c",num[i],i==n?'\n':' ');
}
}
return ;
}
 

hdu 6301的更多相关文章

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

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

  2. HDU 6301 Distinct Values

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

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

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

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

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

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

  6. HDU 6301 (贪心+优先队列)

    题目大意: 求一个长度为n的数列, 给出m个区间,这m个区间各自区间内的数不同 题解: 用优先队列来模拟过程 , 解题思路是想到了 , 可是不知道如何实现 , 果然还须继续努力呀 这道题思路是去掉重复 ...

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

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

  8. hdu 6301 Distinct Values (贪心)

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

  9. 2018 Multi-University Training Contest - Team 1 题解

    Solved A HDU 6298 Maximum Multiple Solved B HDU 6299 Balanced Sequence Solved C HDU 6300 Triangle Pa ...

随机推荐

  1. 在docker上centos7 编译安装php7

    docker镜像来自daocloud.io/library/centos 首先下载libmcrypt库并make && make install yum -y install gcc ...

  2. ajax请求拿到多条数据拼接显示在页面中

    首先我们拿到的了一坨Json数据 如下 然后通过ajax请求拿到数据 在ajax的success方法中处理和使用数据: 其中包括: 用eval处理这种数据 var outStr = eval('('+ ...

  3. 文件操作函数及光标,tell,truncate

    一丶文件基本操作 1.打开文件 open(文件名(路径),mode = "?",encoding = "字符集") 2.文件路径: 1.绝对路径,从磁盘的根目录 ...

  4. Yii2.0数据库缓存依赖发布的使用理解

    对于产品中经常需要生成一些缓存类的东西,比如系统基础配置,商品分类等,每次修改调整后都要手动进行缓存发布,是不是非常麻烦!这时候Yii2.0的缓存依赖发布就起到至关重要的作用了!现将主要的使用流程介绍 ...

  5. Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)'''from seleni ...

  6. 将C4C Service Request中的summary和其他附件同步到ERP的Billing Request去

    C4C里将Service Request称为Work Ticket. 比如现在我的Service Request有两个行项目,只有第一个需要同步到ERP去.但是第二个行项目对于客户检查Invoice来 ...

  7. Python01 VSCode开发环境和入门程序

    1.Python的下载和安装 最新版本python3.7.3 https://www.python.org/downloads/release/python-373/ web-based: 在线安装包 ...

  8. PhoneGap+JQuery Mobile移动应用开发学习笔记

    最近一直在学习使用PhoneGap+JQuery Mobile的开发框架开发Android应用,抛开这个框架的运行效率不说,暂且将使用中遇到的问题进行一下整理. 1.JS文件引用顺序 也许在进行web ...

  9. 关于SQL语言的初步认识

    关于SQL语言的初步认识 1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义. 2.一个SQL表由行集构成,一行是列的序列(集合),每列与行对应一个数据项. 3.一个表或者是一 ...

  10. 使用的是html5的canvas将文字转换成图片

    当前功能的运用场景是:用户需要传文件给他人,在用户选择文件之后需要显示一个文件图标和所选文件的名称. 当前代码部分是摘自网上,但是已经忘记在什么地方获取的,如有侵权联系小弟后自当删除. 注意:必须在h ...