Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2558 Accepted Submission(s): 820
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
题意:
给出m个区间,区间内的元素不能重复,求一个n长度的数组
算法:
贪心、优先队列
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll int
struct T{
ll x,y;
}node[100005];
bool cmp(T a,T b){
if (a.x == b.x)
return a.y > b.y;
return a.x < b.x;
}
ll mark[100005];
int main(){
ll t;
scanf("%d",&t);
while (t--) {
ll n,m;
scanf("%d %d",&n,&m);
for (ll i = 1;i <= m;i++){
scanf("%d %d",&node[i].x,&node[i].y);
}
sort(node+1,node+m+1,cmp);
priority_queue<int,vector<int>,greater<int> > q;
for (ll i = 1;i <= n;i++){
q.push(i);
}
for (int i = 0;i <= n;i++){
mark[i] = -1;
}
int ml = 1;
int mr = 1;
for (ll i = 1;i <= m;i++){
if (i==1 | node[i-1].x != node[i].x ){
for (int j = ml; j < node[i].x; j++){
if (mark[j] != -1){
q.push(mark[j]);
}
else {
mark[j] = 1;
}
}
for (int j = mr; j <= node[i].y; j++){
if (mark[j] == -1){
mark[j] = q.top();
q.pop();
}
}
ml = node[i].x;
mr = max(mr, node[i].y);
}
}
for (int i = 1; i < n; i++){
if (mark[i] == -1){
mark[i] = 1;
}
printf("%d ",mark[i]);
}
if(mark[n] == -1) mark[n] = 1;
printf("%d\n",mark[n]);
}
}