Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3261 Accepted Submission(s): 1058
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
2018 Multi-University Training Contest 1
Recommend
liuyiding | We have carefully selected several similar problems for you: 6308 6307 6306 6305 6304
//1-n个数,初始为1,给你m个区间,要求区间内的数不同,问你字典序最小的怎么排
//开了一个set来维护每次可用的数,优先队列可能会有重复,
//set取第一个就是 s.*begin()就可以了
//不能直接用后一个序列跟前一个序列去比,因为可能会出现前一个序列全包含在上一个序列的情况
//开一个L,R,去维护实际区间
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn];
set <int> s;
struct node
{
int l,r;
} p[maxn];
bool cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,L,R;
scanf("%d%d",&n,&m);
for(int i=1; i<=m; i++)
{
scanf("%d%d",&p[i].l,&p[i].r);
}
sort(p+1,p+m+1,cmp);
for(int i=1; i<=n; i++)
{
s.insert(i);
a[i]=1;
}
for(int i=p[1].l; i<=p[1].r; i++)
{
int u=*s.begin();
a[i]=u;
s.erase(u);
}
L=p[1].l,R=p[1].r;
for(int i=2; i<=m; i++)
{
if(p[i].l>=L&&p[i].r<=R) continue;
else if(p[i].l>R)
{
for(int j=L; j<=R; j++) s.insert(a[j]);
for(int j=p[i].l; j<=p[i].r; j++)
{
int u=*s.begin();
a[j]=u;
s.erase(u);
}
L=p[i].l,R=p[i].r;
}
else
{
for(int j=L; j<=p[i].l-1; j++) s.insert(a[j]);
for(int j=R+1; j<=p[i].r; j++)
{
int u=*s.begin();
a[j]=u;
s.erase(u);
}
L=p[i].l,R=p[i].r;
}
}
for(int i=1; i<=n; i++)
{
if(i==1) printf("%d",a[i]);
else printf(" %d",a[i]);
}
printf("\n");
}
return 0;
}