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≠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

题意是说第一行输入一个n,m,n代表数组的长度,m代表下面的m行;下面的m 行每一行是li和ri,代表从li~ri之间的数据不能一样,输出的是长度为n的最小的数组;(例如:第三组数据,表示数组长度为5,最大的数组为1 2 3 4 5;下面有2行,第一行表示1~3之间的数据不能相同,还要保持输出的数组是最小,即1~3为1 2 3 ;第二行表示2~4的数据不能相同,还要保持输出的数组最小,即2~4为2 3 1;最后一位没有要求,即保持最小为1,综上,输出为1 2 3 1 1)

需要用到的知识是STL中的set,以及区间的相关知识。

代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    int pre[100005];
    int ans[100005];
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            pre[i]=i;//初始定义区间的左界
        for(int i=0;i<m;i++)
        {
            int l,r;
            cin>>l>>r;
            pre[r]=min(pre[r],l); //通过每一次的输入,比较区间的左界,取左界的最小值
        }
        for(int i=n-1;i>=1;i--)
            pre[i]=min(pre[i],pre[i+1]);
        set<int>s;
        for(int i=1;i<=n;i++)
            s.insert(i);
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            while(flag<pre[i])//当flag比i的左界还小的话,说明flag所指向的数对i的区间没有影响
            {
                s.insert(ans[flag]);//此时就要把flag指向的数送回set中,即i得区间内可以使用这个数
                flag++;//直到flag进入到i的左界时,跳出循环,执行set
            }
            ans[i]=*s.begin();
            s.erase(ans[i]);//某个数用过之后就需要用erase删除,即这个数不能再使用,以免重复
        }
        for(int i=1;i<n;i++)
            cout<<ans[i]<<" ";
        cout<<ans[n]<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值