Distinct Values
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 36 Accepted Submission(s) : 17
Problem Description
Chiaki has an array of $n$ positive integers. You are told some facts about the array: for every two elements $a_i$ and $a_j$ in the subarray $a_{l..r}$ ($l \le i < j \le r$), $a_i \ne a_j$ holds.
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 \le n, m \le 10^5$) -- the length of the array and the number of facts. Each of the next $m$ lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$). It is guaranteed that neither the sum of all $n$ nor the sum of all $m$ exceeds $10^6$.
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
这道题的意思是组成最小的数,需要用到STL排序。
#include<set>
#include<bits/stdc++.h>
const int N = 5e5 + 10;
const int INF=0x3f3f3f3;
using namespace std;
int a[N],pre[N];//用来记录下标,代表着区间里的元素不能重复;
int main()
{
int i,m,n;
int t,qi,hou;//qi代表区间的前边界,hou代表区间的后边界;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=1; i<=m; i++)
{
pre[i]=i;
}
for(i=1; i<=n; i++)
{
scanf("%d%d",&qi,&hou);
pre[hou]=min(pre[hou],qi);
}
for(i=m-1; i>=1; i--)
{
pre[i]=min(pre[i],pre[i+1]);把同意区间的元素都标记为区间的前元素,代表同属一个集合,不能重复
}
set<int> s;
int p=1;
for(i=1; i<=m; i++)
{
s.insert(i);
}
for(i=1; i<=m; i++)
{
while(p<pre[i])//p代表下表,如果p小于区间的前元素,则不属于这个区间,讲p对应的元素传到s里面
{
s.insert(a[p]);
p++;
}
a[i]=*s.begin();
s.erase(a[i]);
}
for(i=1; i<=m; i++)
{
if(i==1)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
puts("");
}
return 0;
}