Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3244 Accepted Submission(s): 1051
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,代表数组长度,然后给定m,代表有m组要求,每组要求给定l,r,代表l到r区间的数字两两不能相同,要求数组填充的最小值为1,问最小字典序的结果是什么?
题解:构造一个优先队列,优先队列一开始存放1-n,然后按左区间升序,相同情况按右区间降序。然后记录一个l代表上一个区间的左端点,curr代表右端点。则有三种情况
1、下一个区间的右端点小于等于curr,则该区间被覆盖,无任何操作。
2、下一个区间的左端点大于curr,则该区间与当前区间无任何接触,释放之前区间的值,然后不断pop优先队列直到将新区间填满。
3、下一个区间的左端点小于等于curr,则释放l~左端点-1的值,在curr+1~右端点不断pop优先队列直到将新区建填满。
AC代码
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#define ll long long
using namespace std;
struct node{
int x,y,id;
node(int a, int b, int c){
x = a;
y = b;
id = c;
}
};
bool cmp(node a1, node a2){
if(a1.x!=a2.x)
return a1.x < a2.x;
return a1.y < a2.y;
}
vector<node> s;
int main(){
int t,i,n,a ,b;
while(scanf("%d", &t)!=EOF){
while(t--){
s.clear();
scanf("%d", &n);
for(int i = 0; i < n; i++){
for(int j = 0; j < 3; j++){
scanf("%d %d", &a, &b);
s.push_back(node(a, b, i * 3 + j + 1));
}
}
sort(s.begin(),s.end(),cmp);
for(int i = 0; i < n; i++){
printf("%d %d %d\n",s[i*3+0].id, s[i*3+1].id, s[i*3+2].id);
}
}
}
return 0;
}