hdu多校1004 Distinct Values

Distinct Values
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2635    Accepted Submission(s): 849


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
 

Source
2018 Multi-University Training Contest 1
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6308 6307 6306 6305 6304 
 

用优先队列。先对x排个序。

不重叠 就从1到n开始复制

重叠   把和上一段没有重叠部分加入到 优先队列当中。然后队列不为空时就从队列里出。不然就上一个的最大值++;

#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<functional>
#include<algorithm>
using namespace std;
priority_queue <int,vector<int>,greater<int> > q;
struct node
{
    int x;
    int y;
} a[100005];
int ans[100005];
bool cmp(node a,node b)
{
    return a.x<b.x;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            ans[i]=1;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        sort(a+1,a+1+m,cmp);
        int l=0,r=0;
        while(!q.empty())
        {
            q.pop();
        }
        int maxx=1;
        for(int i=1; i<=m; i++)
        {
            if(a[i].y<=r)
            {
                continue;
            }
            if(a[i].x>r)
            {
                for(int j=a[i].x,k=1; j<=a[i].y; j++,k++)
                {
                    ans[j]=k;
                    maxx=k;
                }
                while(!q.empty())
                {
                    q.pop();
                }
                l=a[i].x;
                r=a[i].y;

            }
            else
            {
              for(int j=l;j<a[i].x;j++)
              {
                q.push(ans[j]);
              }
              for(int j=r+1;j<=a[i].y;j++)
              {
                  if(!q.empty())
                  {
                      ans[j]=q.top();
                      q.pop();
                  }
                  else
                  {
                      ans[j]=(++maxx);
                  }
              }
              l=a[i].x;
                r=a[i].y;
            }
        }
        for(int i=1; i<n; i++)
            cout<<ans[i]<<" ";
        cout<<ans[n]<<endl;

    }
    return 0;
}
原文地址:https://www.cnblogs.com/2014slx/p/9360734.html