Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3285 Accepted Submission(s): 1064
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≠aj 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≤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
开始之前,先说一句,杜教牛逼!
好了,补题~
题目意思是,让你构造一段字符串,满足以下规则:
1,构造出的数字尽量小,比如第一组样例答案是数字12,只能使用1 ~ 9;
2,题目给出一些区间,要求这个区间内的数字不能重复
思路是 ;
第一步,记录区间,并且处理掉无效的区间,正常都是想到用结构体数组或者pair来装,但是杜神用了一个方法,一个数组,下标作为区间的右边界,值记录左边界,然后取最小值,这样,就解决了区间覆盖的问题,数组要初始化为pre[i] = i。
第二部,开始按照贪心策略去填数字,代码中的pl变量很重要,记录的是上一次回收到的位置,例如2 4区间
然后杜神还用了set来取当前可用的最小值,和优先队列使用方法一样,但是好像简单一点。
O(n)扫过去答案就出来了。
【rep什么的真好用】
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
const int N = 100100;
int main (){
int T;
scanf("%d",&T);
int m,n;
int pre[N];
int ret[N];
int l,r;
while(T--){
scanf("%d %d",&n,&m);
for(int i = 0;i <= n;i++) pre[i] = i;
for(int i = 0;i < m;i++){
scanf("%d %d",&l,&r);
pre[r] = min(pre[r],l);
}
// cout << endl;
// rep(i,1,n + 1) cout << i << " " << pre[i] << endl;
per(i,1,n) pre[i] = min (pre[i],pre[i + 1]);
// cout << endl;
// rep(i,1,n + 1) cout << i << " " << pre[i] << endl;
int pl = 1;
set<int> val;
rep(i,1,n + 1) val.insert(i); // 填充set
rep(i,1,n + 1){
while(pl < pre[i]){ // pl为上一次回收到的位置
// cout << "pl : " << pl << " ret[pl] : " << ret[pl] << endl;
val.insert(ret[pl]);
pl++;
} // 回收可用数字
ret[i] = *val.begin(); // 当前位置永远从set中提取最小的答案
val.erase(ret[i]);
}
rep(i,1,n+1) printf("%d%c",ret[i]," \n"[i==n]);
}
}