2018 Multi-University Training Contest 1 D(Distinct Values)

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
 

题意:数量为N的序列,给定M个区间,要求对每个区间Li,Ri,都有al..r (l≤i<j≤r), ai≠aj。构造这个序列使其字典序最小。

思路:

来自:https://blog.csdn.net/xbb224007/article/details/81179319

(1)如果某个位置没有被区间覆盖过,那么这个位置填1。
(2)如果一个区间完全A包含在另外一个B内,那么区间A不需要被考虑。
(3)如果两个区间部分重叠(区间A在前,B在后--按左端点排序),那么重叠部分的数字不需要被考虑,而A中未重叠部分的数字可以被重新填入B的为重叠部分。
(4)因为要保证字典序尽可能小,所以每次在设置某个位置的值的时候,我们都尽可能地选取小的数。
(5)用优先队列或者set维护一下当前可以填放的最小的数即可。

补充:维护一个j表示当set中的数被用完时,从j向后取数填充。如果前后两区间不相交j变回1。

队友的代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int n,m,k;
int ans[maxn],in[maxn];
int ct,cnt,tmp,flag;
struct node{
    int l,r;
    bool operator<(node aa)const{
        return l<aa.l||l==aa.l&&r<aa.r;
    }
}a[maxn];
int main(){
    priority_queue<int,vector<int>,greater<int> >q;
    priority_queue<int,vector<int>,greater<int> >p;
    int T,cas=1;
    scanf("%d",&T);{
        while(T--){
        q=p;
        memset(in,0,sizeof(in));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++) scanf("%d %d",&a[i].l,&a[i].r);
        sort(a,a+m);
        int j=1;
        int k=a[0].l;
        int pos=a[0].l;
        for(int i=1;i<pos;i++) ans[i]=1;
        bool jud=0;
        for(int i=0;i<m&&pos<=n;i++)
        {
            int l=a[i].l;
            if(pos<l&&pos<=n){j=1;ans[pos++]=1;k=a[i].l;
            while(!q.empty()){in[q.top()]=0;q.pop();}
            }
            while(pos<l&&pos<=n){ans[pos++]=1;}
            if(pos>n) break;
            while(k<l)
            {
                if(!in[ans[k]]){q.push(ans[k]);in[ans[k]]=1;}
                k++;
            }
            while(pos<=a[i].r)
            {
                if(!q.empty()){
                ans[pos]=q.top();q.pop();
                in[ans[pos]]=0;
                }
                else ans[pos]=j++;
                pos++;
            }
        }
        for(int i=pos;i<=n;i++)ans[i]=1;
        for(int i=1;i<=n;i++)
        {
            printf("%d%c",ans[i],i==n?'\n':' ');
        }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/snayf/article/details/81210322