2018 Multi-University Training Contest 1 Distinct Values

                                         Distinct Values

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


 

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

                                                                      Statistic | Submit | Clarifications | Back

题意:

长度为n的序列,有m个范围,每个范围内的数字不能相同,要求字典序最小,也就是说范围没覆盖的地方全是一就行了,范围内的从小到大

解题思路:

这个复杂度我觉得是2*m*n但是不知到为什么能过,还写了一个也是这个复杂度的但是超时,现在说说这个做法,设置一个左指针和右指针,左指针l若小于范围的左括号,意味着l位置可以释放该数字到一个优先队列里(这里事先准备了1到n,放到优先队列里,用一个弹出一个,也不断补充,队列设置为从小到大),也就是该数字可用,但是这个位置是0的时候就不要弹到队列里,因为本来就没放数,r的话如果小于范围的右括号,就从队列里弹出元素放入该位置,这里注意,如果当前的r小于此范围的l则不从队列里弹出,就无视。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct pt
{
    int l,r;
} p[100003];
bool cmp(pt x,pt y)
{
    if(x.l==y.l)return x.r>y.r;
    return x.l<y.l;
}
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
    int n,m,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&p[i].l,&p[i].r);
        }
        sort(p,p+m,cmp);
        while(!q.empty())q.pop();
        int u=1;
        while(u<=n)
        {
            q.push(u++);
        }
        int r=0,l=1;
        int a[100002]= {0};
        for(int i=0; i<m; i++)
        {
            while(l<p[i].l)
            {
                if(a[l]!=0)q.push(a[l]);
                l++;
            }
            while(r<p[i].r)
            {
                r++;
                if(r>=p[i].l)
                {
                     int u=q.top();
                q.pop();
                a[r]=u;

                }

            }
        }
        if(a[1]==0)a[1]=1;
        printf("%d",a[1]);
        for(int i=2; i<=n; i++)
        {
            if(a[i]==0)a[i]=1;
            printf(" %d",a[i]);
        }
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/wrwhahah/article/details/81204933