Distinct Values

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

题目大意:给出n个数(1~n)和m个区间,让你给每个区间去重标记(大概是这样)。例如样例3:1~5,给出2个区间,

第一个区间是1-3,因为刚开始1-5都未编号,所以1-3编号1-3,第二个区间2-4,因为2,3已经编号为2,3。。4没有被编号,且2-4有3个数,所以这3个数应该用有1-3的编号,编号2,3已有归属,所以4的编号为1。   然后还剩与一个5(区间),依次编号1,2,3;

所以为1.2.3.1.1

思路:从左至右小区间依次操作。

代码如下:(大佬代码加了注释)

#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++)
        {
            while(l<v[i].u-1)
            {
                now.date=a[l];//数组a开始时无值,有值时 
                q.push(now);
                l++;
            }//标记l从左到右入队 ,更新为上一个区域而定左边界 
            while(j<v[i].to)//j小于右边界 
            {
                if(q.empty())//当栈为空时 (从0开始所以有一个错位)
                {
                    a[j++]=str;//按顺序赋值 
                    str++; 
                }
                else
                {
                    now=q.top();//最大堆,最大元素 
                    a[j++]=now.date;
                    q.pop();
                }
            }//标记j从右至左,更新为上一个区域的右边界 
        }
        printf("%d",a[0]);
        for(i=1;i<n;i++)
        printf(" %d",a[i]);
        printf("\n");
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值