Distinct Values(不同价值)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41181771/article/details/81271359

题目链接
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 (1≤i≤r,1≤j≤r,j>i), 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
题意分析:
给出一个数组长度和数据组数,每组数据有个l和r,al~ar中不能有任何两个数据是相等的,且都为整数,故应从1开始放入,使用迭代器set或者优先数列都是可以的。
注意:
哇,这题提交的时候真的是费了我不少力气
首先,杭电是不吃“bit/stdc++.h”这个万能头文件的,其次,杭电还不吃min和max的小函数(不能快捷使用min和max),所以一直编译错误….还有,题目上说的数据是10的6次方,数组要开的足够大才能过….不多说了,看代码吧..
代码篇:

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
int a[10010],pre[10010];
int main()
{
    int n,m,i,l,r,t;
    cin >> t;
    while(t--)
    {
        int flag=1;
        set<int>s;
        cin >> n >> m;
        for(i=1; i<=n; i++)
        {
            pre[i]=i;///初始化为1.2.3.....
            s.insert(i);///set里面也是
        }
        while(m--)
        {
            cin >> l >> r;
            pre[r]=(pre[r]<l)?pre[r]:l;///进入小的
        }
        for(i=n-1; i>=1; i--)
            pre[i]=(pre[i]<pre[i+1])?pre[i]:pre[i+1];///左界的推移
        for(i=1; i<=n; i++)
        {
            while(flag < pre[i])
            {
                s.insert(a[flag]);
                flag++;
            }
            a[i]=*s.begin();///赋值
            s.erase(a[i]);///擦除
        }
        cout << a[1];
        for(i=2; i<=n; i++)
        {
            printf(" %d",a[i]);
        }
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41181771/article/details/81271359