Distinct Values

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

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
 

  比赛时候想到了用set找,又感觉复杂度爆炸,下来想想的话,每个点最多进出一次所以O(N*log(N))还是ok的。

就是排序区间然后枚举所有区间,一段一段的填充,要维护一个set表示当前区间段内所有能写入的数。还要记录下上一个区间。

 #include<bits/stdc++.h>
using namespace std;
struct node{
int l,r;
}A[];
int a[];
bool cmp(node A,node B){
if(A.l!=B.l) return A.l<B.l;
return A.r>B.r;
}
set<int>S;
set<int>::iterator it;
int main(){
int t,n,m,i,j,k;
cin>>t;
while(t--){
S.clear();
memset(a,,sizeof(a));
scanf("%d%d",&n,&m);
for(i=;i<=n;++i) S.insert(i);
for(i=;i<=m;++i) scanf("%d%d",&A[i].l,&A[i].r);
sort(A+,A++m,cmp);
int l=A[].l,r=A[].r;
for(i=l,j=;i<=r;++i,++j) a[i]=j,S.erase(j);
for(i=;i<=m;++i){
if(A[i].r<=r) continue;
if(A[i].l>r){
for(j=l;j<=r;++j) S.insert(a[j]);
for(j=A[i].l;j<=A[i].r;++j){
a[j]=*S.begin();
S.erase(a[j]);
}
l=A[i].l,r=A[i].r;
}
else{
for(j=l;j<A[i].l;++j) S.insert(a[j]);
for(j=r+;j<=A[i].r;++j){
a[j]=*S.begin();
S.erase(a[j]);
}
l=A[i].l,r=A[i].r;
}
}
for(i=;i<=n;++i) printf("%d%c",a[i]==?:a[i],i==n?'\n':' ');
}
return ;
}

hdu-6301-贪心的更多相关文章

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

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

  2. hdu 6301 Distinct Values (贪心)

    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 (贪心+优先队列)

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

  5. HDU 6301 Distinct Values

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

  6. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  7. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  8. hdu 1735(贪心) 统计字数

    戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...

  9. hdu 4974 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...

  10. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

随机推荐

  1. 学习Linux的正确姿势

    学习Linux的正确姿势 端正学习态度1.Linux不等于骇客(or Cracker).当然众所周知很多“黑客工具”都是Linux平台上的,我帮助过很多Linux小白发现他们殊途同归都是朝着类似Air ...

  2. web前端----jQuery属性操作

    知识点总结 1.属性 属性(如果你的选择器选出了多个对象,那么默认只会返回出第一个属性). attr(属性名|属性值) - 一个参数是获取属性的值,两个参数是设置属性值 - 点击加载图片示例 remo ...

  3. IDEA上传代码到码云

  4. nginx服务器的rewrite功能

    nginx编译的时候默认是把rewrite功能编辑进去的,但是因为rewrite需要使用正则表达式,因此需要安装pcre依赖包. yum install -y pcre pcre-install re ...

  5. JavaScript Image对象 / Tabel对象 / Select对象 / Form对象

    JavaScript Image / Tabel / Select / Form 对象 版权声明:未经授权,严禁转载! Image 对象 Image 对象,代表 <img> 元素. < ...

  6. Log4j将不同Package的日志输出到不同的文件的方法

    随着项目规模的越来越大,会不断的引入新的模块,不同的模块都会打印自己的日志,最后就造成日志根本没法查看,比如我自己的项目中,就存在以下这些日志: 接收外界消息的日志.对外发送消息的日志: 后台常驻线程 ...

  7. ubuntu 安装 ftp服务

    1. 更新源列表 ---> sudo apt-get update 2. 安装vsftpd ---> sudo apt-get install vsftpd (安装) ----> s ...

  8. 《EMCAScript6入门》读书笔记——24.编程风格

  9. Facebook广告API系列 2 - Audience Management

    Facebook广告API系列 2 Facebook marketing API有三大组成部分: Audience Management Ads Management Ads Insights 本篇稍 ...

  10. 全网最详细Apache Kylin1.5安装(单节点)和测试案例

    转:http://blog.itpub.net/30089851/viewspace-2121221/ 微视频链接: Apache Kylin初识      1.版本(当前实验版本组合,版本一定要兼容 ...