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

分析:想法很暴力,安区间左端点排序,左端点一样按右端点排序,因为要字典序最小,尽量填小的,贪心思想,把前面的区间先搬到优先队列里,在从优先队列里往后搬

#include<bits/stdc++.h>
using namespace std;
int a[100005];
struct node
{
    int date;
    bool operator<(const node &aa)const
    {
        return date>aa.date;
    }
};
priority_queue<node>q;
struct line
{
    int u,to;
    bool operator<(const line &aa)
    {
        if(u!=aa.u)
        return u<aa.u;
        return to<aa.to;
    }
}v[200005];
int main()
{
    int t,i,j,str,n,m,l;
    node now;
    scanf("%d",&t);
    while(t--)
    {
        while(!q.empty())
        {
            q.pop();
        }
        scanf("%d%d",&n,&m);
        str=1;
        for(i=0;i<m;i++)
            scanf("%d%d",&v[i].u,&v[i].to);
        for(j=i,i=1;i<=n;i++,j++)
            v[j].u=v[j].to=i;
        m=j;
        sort(v,v+m);
        for(i=0,j=0,l=0;i<m&&j<n;i++)
        {
           // printf("aaaa %d %d\n",l,v[i].u);
            while(l<v[i].u-1)
            {
                now.date=a[l];
                q.push(now);
                l++;
            }
            while(j<v[i].to)
            {
                if(q.empty())
                {
                    //printf("ssss  %d %d\n",j,str);
                    a[j++]=str;
                    str++;
                }
                else
                {
                    //printf("ok  %d %d\n",j,str);
                    now=q.top();
                    a[j++]=now.date;
                    q.pop();
                }
            }

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

    }
}

猜你喜欢

转载自blog.csdn.net/qq_37891604/article/details/81182083