【题目】
Distinct ValuesTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4054 Accepted Submission(s): 1360 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.
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:
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个区间,要求给定的区间内要素不同,求字典序最小的序列。
思路:运用贪心的思想把问题转化为寻找区间内最小的没有出现过的正整数。排序处理下区间,用优先队列维护即可。
【代码】
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
struct p{
int l,r;
}f[maxn];
int ans[maxn];
bool cmp(p a,p b)
{
if(a.l==b.l)
return a.r>b.r;
return a.l<b.l;
}
int main()
{
int t; scanf("%d",&t);
while(t--)
{
priority_queue <int,vector<int>,greater<int> > q;
int n,m; scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
q.push(i),ans[i]=1;
for(int i=0;i<m;i++)
scanf("%d%d",&f[i].l,&f[i].r);
sort(f,f+m,cmp);
int bg=1,ed=1;
q.pop();
for(int i=0;i<m;i++){
if(f[i].l>=bg&&f[i].r<=ed) continue;
if(f[i].l>ed){
for(int j=bg;j<=ed;j++)
q.push(ans[j]);
for(int j=f[i].l;j<=f[i].r;j++)
ans[j]=q.top(),q.pop();
}
else{
for(int j=bg;j<f[i].l;j++)
q.push(ans[j]);
for(int j=ed+1;j<=f[i].r;j++)
ans[j]=q.top(),q.pop();
}
bg=f[i].l;
ed=f[i].r;
}
for(int i=1;i<=n;i++)
printf(i<n?"%d ":"%d\n",ans[i]);
}
return 0;
}