Distinct Values

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

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
 

题意:你需要构造一个长度为n的字典序最小的序列,要求如下

   1,有m个区间,在这些区间内,数字不得重复

   2,长度为n,不能有0出现

题解:既然要求字典序最小,那么我们那些没有被区间覆盖的地方一定是1

   区间覆盖的地方得特殊考虑,区间是否相交(重叠)等问题

   于是我们先把区间按照左端点排序,每次在区间内放尽可能小的就行

   格式要求行尾不能有多余的空格

代码如下

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
struct node{
int l,r;
}a[maxn];
bool cmp(node a,node b){
if(a.l==b.l) return a.r<b.r;
return a.l<b.l;
}
int ans[maxn];
int vis[maxn];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+,a+m+,cmp);
priority_queue<int,vector<int>,greater<int> >q;
for(int i=; i<=n; i++){
ans[i]=;vis[i]=;
q.push(i);
}
int l=a[].l,r=a[].l;
for(int i=;i<=m;i++){
for(;l<a[i].l;l++){
if(vis[l]) q.push(ans[l]);
}
for(;r<=a[i].r;r++){
if(r>=a[i].l){
ans[r]=q.top();q.pop();
vis[r]=;
}
}
}
for(int i=; i<=n; i++) {
printf("%d",ans[i]);
if(i==n) printf("\n");
else printf(" ");
}
}
}

HDU 多校对抗赛 D Distinct Values的更多相关文章

  1. HDU 多校对抗赛第二场 1004 Game

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDU 多校对抗赛第二场 1010 Swaps and Inversions

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU 多校对抗赛 C Triangle Partition

    Triangle Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Oth ...

  5. HDU 多校对抗赛 A Maximum Multiple

    Maximum Multiple Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 多校对抗赛 J Time Zone

    Time Zone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. hdu多校1004 Distinct Values

    Distinct Values Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): ...

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

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

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

随机推荐

  1. Java学习笔记七:Java的流程控制语句之switch

    Java条件语句之 switch 当需要对选项进行等值判断时,使用 switch 语句更加简洁明了.例如:根据考试分数,给予前四名不同的奖品.第一名,奖励笔记本一台:第二名,奖励 IPAD 2 一个: ...

  2. ctf题目writeup(1)

    2019/1/28 题目来源:爱春秋 https://www.ichunqiu.com/battalion?t=1 1. 该文件是一个音频文件: 首先打开听了一下,有短促的长的....刚开始以为是摩斯 ...

  3. A*与IDA*的奇妙之旅

    因为A*卡了一天QAQ 那么,A*是什么呢? A* A*是对于bfs的优化,启发式搜索. 例如下图: 不错,在这张图上,小人去找电脑,用bfs的话: 黄色就是bfs的搜索范围...不要问我为什么选黄色 ...

  4. spfa专题

    SPFA专题 1通往奥格瑞玛的道路 在艾泽拉斯,有n个城市.编号为1,2,3,...,n. 城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量. 每 ...

  5. 面试-MySQL总结

    三范式 三范式定义(范式和反范式) 1NF:每个数据项都是最小单元,不可分割,确定行列之后只能对应一个数据. 2NF:每一个非主属性完全依赖于候选码(属性组的值能唯一的标识一个元组,但是其子集不可以) ...

  6. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

  7. Use Matlab though C++

    0. Environment Windows 8.1 Pro x64 Matlab R2013a 32-bit (installed in "F:\ProgramFiles_x86\MATL ...

  8. APK反编译后添加日志

    一.反编译 参考前一篇文章 二.添加寄存器(locals) 因为要添加日志,我们一般需要用一个变量来存储TAG,所以需要增加一个寄存器 如: # virtual methods .method pub ...

  9. Python简要标准库(5)

    hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 基本的生成MD密匙的函数 import hashlib md5 = hashlib.md5() md5.up ...

  10. AGV系统上位机--工程案例【1】

    1.满足80%系统需求,根据需求生成任务表单 2.指定小车下方任务 3.项目实战应用 4.后台开发,对接其他平台,简易实现自动生成任务列表,自动排单 5.AGV系统上位机初学者很容易理解上手 6.欢迎 ...