杭电多校第一场 Distinct Values(贪心+set)

Distinct Values

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2922    Accepted Submission(s): 946


 

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

 

Source

2018 Multi-University Training Contest 1

 

Recommend

liuyiding

题意:一个大小为n的数组,给出m个区间,要求这m个区间的每一个区间里的不能出现相同的数,输出字典序最小的符号要求的数组。

思路:贪心,把m个区间按照L排序,根据当前的区间和上一个区间的位置关系来向数组里填数,有点莫队的思想,用set来维护要填的数

#include <stdio.h>
#include <algorithm>
#include <set>

using namespace std;
const int MAXN = 100005;

struct node
{
    int l,r;
}q[MAXN];
set<int> st;
bool cmp(const struct node &a,const struct node &b)
{
    if(a.l == b.l) return a.r < b.r;
    else return a.l < b.l;
}
int ans[MAXN];
int main(void)
{

    int T;
    scanf("%d",&T);
    while(T--) {
        int n,m;
        scanf("%d%d",&n,&m);
        st.clear();
        for(int i = 1; i <= n; i++) {
            st.insert(i);
        }
        for(int i = 1; i <= m; i++) {
            scanf("%d %d",&q[i].l,&q[i].r);
        }
        sort(q + 1,q + 1 + m,cmp);
        int pre_r = 0,pre_l = 0;
        for(int i = 1; i <= m; i++) {
            if(pre_r == 0 && pre_l == 0) {
                for(int j = 1; j <= q[i].l - 1; j++) ans[j] = 1;
                for(int j = q[i].l; j <= q[i].r; j++) {
                    ans[j] = (*st.begin());
                    st.erase(*st.begin());
                }
                pre_r = q[i].r;
                pre_l = q[i].l;
            }
            else if(q[i].l > pre_r) {
                while(pre_r < q[i].l - 1) ans[++pre_r] = 1;
                while(pre_l < q[i].l) st.insert(ans[pre_l++]);
                while(pre_r < q[i].r) {
                    ans[++pre_r] = (*st.begin());
                    st.erase(*st.begin());
                }
            }
            else if(q[i].l <= pre_r) {
                while(pre_l < q[i].l) st.insert(ans[pre_l++]);
                while(pre_r < q[i].r) {
                    ans[++pre_r] = (*st.begin());
                    st.erase(*st.begin());
                }
            }
        }
        while(pre_r < n) ans[++pre_r] = 1;
        for(int i = 1; i <= n; i++) {
            if(i == n) printf("%d\n",ans[i]);
            else printf("%d ",ans[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/81191868