hdu多校1004 Distinct Values

Posted 2014slx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于hdu多校1004 Distinct Values的主要内容,如果未能解决你的问题,请参考以下文章

HDU多校(Distinct Values)

HDU 6301 Distinct Values

2018 杭电多校1 - Distinct Values

hdu多校 1004 1007 1010

HDU6312 Game (多校第二场1004) 简单博弈

hdu多校第五场1004 (hdu6627) equation 1 计算几何