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. 谷歌发布"自动机器学习"技术 AI可自我创造

    谷歌发布"自动机器学习"技术 AI可自我创造 据Inverse报道,今年5月份,谷歌宣布其人工智能(AI)研究取得重大进展,似乎帮助科幻小说中最耸人听闻的末日预言成为现实.谷歌推出 ...

  2. 50个CSS技巧

    这里我工作中收集了10个很不错的CSS技巧,你可以用在你的项目上.它可以帮你很好地整理你的元素并让他们看起来蛮酷的.下面开始我们的内容,希望你会喜欢它.下面是我收集的CSS技巧,希望能帮助到你,感觉收 ...

  3. Python入门之面向对象的__init__和__new__方法

    Python入门之面向对象的__init__和__new__方法

  4. 一个风控计算负载过高到mysql主从拆分暴露的各种设计复杂性问题以及解决方法总结

    在很多系统(包括金融类和非金融类)中,其实有大量的系统在很长的一段时间内(具体多长时间视业务的成功与否而定)都是混合型系统,也就是同时具有OLTP+OLAP的业务.我们说任何形式的存在在特定阶段都是合 ...

  5. 20145104张家明 《Java程序设计》第7周学习总结

    20145104张家明 <Java程序设计>第7周学习总结 教材学习内容总结 第13章 简单认识时间和日期 -时间的度量:GMT.UT.TAI.UTC.Unix.epoch. -UTC:保 ...

  6. weka中算法说明[转]

    1) 数据输入和输出WOW():查看Weka函数的参数.Weka_control():设置Weka函数的参数.read.arff():读Weka Attribute-Relation File For ...

  7. JavaScript 小知识

    1.var 变量 <script type="text/javascript"> var a = "hong"; var fun = functio ...

  8. hdu2552 (浮点数复杂运算的四舍五入)题解

    三足鼎立 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. C#入门经典 第六章 委托

    C#入门经典 第六章 6.6 委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字. 委托的声明指定了一个返回类型和一个参数列表. 在定义了委托后,就可以声明该委托类型的变量. 接着 ...

  10. Windows 上安装 pip

    1 从  https://pypi.python.org/pypi/pip#downloads  下载安装包 pip-9.0.1.tar.gz 2 解压 pip-9.0.1.tar.gz 3 用CMD ...