Distinct Values(HDU6301)

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.

扫描二维码关注公众号,回复: 2635381 查看本文章

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

       题目意思为给一个长度为n的序列,序列有m个限制,限制条件为给出l, r,在区间[l,r]之间所有的数不一样,请求出字典序最小的满足条件的序列。

思路:对于给出的限制,是一个个区间,对于每个区间,只要在区间前面出现过的数,都可以使用,没出现的肯定也可以使用,但是可能有重叠的区间,对于重叠区间,我们只需要先处理前面的区间,然后看后面的区间,后面的区间因为有一部分是·重叠的,可以不需要考虑,我们只需要考虑后面没重叠的区间,后面不重叠那一段,我们可以使用其左区间前面的数来填,也可以用更大的数,因为不能重复还要字典序最小,我们可以利用set的去重和排序来实现。

#include<stdio.h>
#include<iostream>
#include<set>
using namespace std;
const int maxn = 1e5 + 10;
int ans[maxn], pre[maxn];
int main()
{
    int T;
    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;
            scanf("%d%d", &l, &r);
            pre[r] = min(pre[r], l);
        }
        for(int i = n-1; i >= 1; -- i)
        {
            pre[i] = min(pre[i], pre[i+1]);
        }
        int tot = 1;
        set<int>s;
        for(int i = 1; i <= n; ++ i)
        {
            s.insert(i);
        }
        for(int i = 1; i <= n; ++ i)
        {
            while(tot < pre[i])
            {
                s.insert(ans[tot]);
                tot++;
            }
            ans[i] = *s.begin();
            s.erase(ans[i]);
        }
        for(int i = 1; i < n; ++ i)
        {
            printf("%d ", ans[i]);
        }
        printf("%d\n", ans[n]);
    }
    return 0;
}

代码中pre数组保存的是当前位置的最左区间的端点,tot标记的是前一个区间的左端点,区间[tot,pre[i]]之间的ans值是我们可以使用的数据,可以加入set。

猜你喜欢

转载自blog.csdn.net/MALONG11124/article/details/81179024