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. C站投稿映兔源的方法

    (因映兔源也不太稳定了,所以不建议映兔上传,正在找其他视频源代替映兔,另外等待C站大大们的webbt源)(20180226) 测试换文件格式后会不会失效,能坚持几天?http://www.cnblog ...

  2. Putty Technical Note

    转载自:http://libai.math.ncu.edu.tw/bcc16/6/putty/puttyt.html Terminal panel Terminal 面板可調整 PuTTY 對於模擬終 ...

  3. Core Java 1

    p264~p267: 1.程序中可能出现错误:用户输入错误.设备错误.物理限制错误.代码错误 2.如果由于出现错误而使得某些操作没有完成,程序应该:返回一种安全状态,并能够让用户执行一些其他命令: 或 ...

  4. django 项目中使用多数据库 multiple databases

    假如在一个django项目中使用到了不只一个数据库, 其实这在大一点的工程中很常见,比如主从库 那么会涉及到如下一些东西 1, 定义 在settings中的DATABASE中定义会使用到的数据,比如除 ...

  5. PHP_SELF变量解析和重复路径解决

    最近升级PHP到PHP7版本,并重新部署了新的Nginx,启动的时候发现了一个问题,全局变量$_SERVER['PHP_SELF']的值发生了改变,从而影响到代码的功能.因此我们来了解下$_SERVE ...

  6. 《网络攻防》实验九:web安全基础实践

    本次实验在XX同学的指导下完成 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入攻击的基本原理,是从客户端合法接口提交特殊的非法代码,让其注入到服务器端执行业务的SQL中去,进而改变 ...

  7. phpMyAdmin本地文件包含漏洞

    4 phpMyAdmin本地文件包含漏洞 4.1 摘要 4.1.1 漏洞简介 phpMyAdmin是一个web端通用MySQL管理工具,上述版本在/libraries/gis/pma_gis_fact ...

  8. Disruptor学习笔记(一):基本原理和概念

    一.Disruptor基本原理 在多线程开发中,我们常常遇到这样一种场景:一些线程接受用户请求,另外一些线程处理这些请求.比如日志处理中的日志输入和告警.这种典型的生产者消费者场景十分常见,而生产者消 ...

  9. noip2008 真题练习 2017.2.25

    不是有很多可以说的,记住不能边算边取min Code #include<iostream> #include<fstream> #include<sstream> ...

  10. Bootloader之uBoot简介

    本文转载自:http://blog.ednchina.com/hhuwxf/1915416/message.aspx 一.Bootloader的引入 从前面的硬件实验可以知道,系统上电之后,需要一段程 ...