杭电多校第一场 Distinct Values (优先队列)

Distinct Values

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4129    Accepted Submission(s): 1386


 

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

 

Source

2018 Multi-University Training Contest 1

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
#define N 100006
inline int read()
{
    char c;
    int x=0,f=1;
    c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

struct node
{
    int l,r;
    friend bool operator <(node x,node y)
    {
        return x.l==y.l?x.r<y.r:x.l<y.l;
    }
}a[N];
int ans[N];
int pre[N];//pre[i]代表第i未至少放几个数字。
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        priority_queue<int,vector<int>,greater<int> >q;//存储第i位之后没有出现过的数字
        n=read(),m=read();
        int l,r;
        for(int i=1;i<=n;i++)pre[i]=i,q.push(i);
        for(int i=0;i<m;i++)
        {
            l=read(),r=read();
            pre[l]=max(pre[l],r);
        }
        int ou=1;
        for(int i=1;i<=n;i++)
        {
            if(i!=1)
                q.push(ans[i-1]);
            while(ou<=pre[i])
            {
                ans[ou++]=q.top();
                q.pop();
            }
        }
        for(int i=1;i<=n;i++)
          printf("%d%c",ans[i],i==n?'\n':' ');
    }

}

参考博客:https://blog.csdn.net/qq_41608020/article/details/81185772

猜你喜欢

转载自blog.csdn.net/weixin_40894017/article/details/81384920