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. torch7入门(安装与使用)

    http://blog.csdn.net/real_myth/article/details/52291636 1.安装与使用 测试系统是ubuntu14.04LTS,默认安装在-/torch,当然我 ...

  2. Python入门之面向对象的多态和继承

    本章内容 Python面向对象的多态和继承对比 ========================================= 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的 ...

  3. 浅谈elasticsearch 集群

    elasticsearch 集群 摘要: elasticsearch 集群 搭建elasticsearch的集群 现在假设我们有3台es机器,想要把他们搭建成为一个集群 基本配置 每个节点都要进行这样 ...

  4. Python3 itchat实现微信定时发送群消息

    Python3 itchat实现微信定时发送群消息 一.简介 1,使用微信,定时往指定的微信群里发送指定信息. 2,需要发送的内容使用excel进行维护,指定要发送的微信群名.时间.内容. 二.py库 ...

  5. 如何用tomcat发布自己的Java项目

    如何用tomcat发布自己的Java项目 tomcat是什么?它是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器.我们用Java开发出来的web项目,通过tomcat发布出来,别人就可 ...

  6. exp9《网络对抗》web安全基础实践201453331魏澍琛

    201453331魏澍琛web安全基础实践 一.实验过程 1.webgoat开启 2.Injection Flaws练习 Command Injection 原网页中没有注入的地方,那就用burpsu ...

  7. 20145208蔡野 《网络对抗》逆向及BOF基础实践

    20145208蔡野 <网络对抗>逆向及BOF基础实践 逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函 ...

  8. Java基础部分二

    1.&与&& &位运算符,&&逻辑与运算符&&还具有短路的功能,即如果第一个表达式为false,则不再计算第二个表达式 2.switch ...

  9. JavaScript 各种验证收集

    filter或者forEach函数,可能是因为你的浏览器还不够新,暂时不支持新标准的函数,你可以使用如下方式自己定义: if (!Array.prototype.forEach) { Array.pr ...

  10. HDU 2157(矩阵快速幂)题解

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...