Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2373 Accepted Submission(s): 762
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
题解:这题我使用优先队列来写的。先用结构体来存每个区间的范围在进行排序,然后遍历结构体来更新数组。要注意优化。。。。
答案:
#include<bits/stdc++.h>
using namespace std;
struct node{
int l;
int r;
}a[100001];
int cmp(node a , node b){
if(a.l == b.l){
return a.r < b.r;
}else{
return a.l < b.l;
}
}
int main(){
int t,n,m,p,k;
int b[100001],ans[100001];
scanf("%d",&t);
while(t--){
priority_queue<int,vector<int>,greater<int> > q;
b[0] = 1;
scanf("%d %d",&n,&m);
for(int i = 1 ; i <= n ; i++){
b[i] = 1;
ans[i] = 0;
q.push(i);
}
for(int i = 1 ; i <= m ; i++){
scanf("%d %d",&a[i].l,&a[i].r);
}
sort(a+1,a+m+1,cmp);
int mr = a[1].l , ml = a[1].r;
for(int i = 1 ; i <= m ; i++){
if(i == m || a[i].l != a[i+1].l ){
for(int j = mr ; j <= a[i].r ; j++){
if((b[j]==1&&ans[j]==0)||(ans[j]&&j>mr)){
b[j] = q.top();
q.pop();
ans[j] = 1;
}
}
mr = max(a[i].r,mr);
if(mr<a[i+1].l){
mr = a[i+1].l;
}
for(int j = a[i].l ; j < a[i+1].l ; j++){
if(q.top() != b[j])
q.push(b[j]);
}
}
}
for(int i = 1 ; i <= n ; i++){
if(i==1){
printf("%d",b[1]);
}else{
printf(" %d",b[i]);
}
}
printf("\n");
}
}