HDU 6301Distinct Values 【贪心】

Distinct Values

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


 

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

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

Sample Output

 

1 2 1 2 1 2 1 2 3 1 1

#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 7;
int pre[MAX];
int ans[MAX];
int main(){
    int N;
    for(scanf("%d", &N); N; N--){
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 1; i < n + 1; i++){
            pre[i] = i;
            ans[i] = 1;
        }
        for(int i = 0, l, r; i < m; i++){
            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 p = 1;
        set <int> s;
        for(int i = 1; i < n + 1; i++)
            s.insert(i);
        for(int i = 1; i < n + 1; i++){
            while(p < pre[i]){
                s.insert(ans[p]);
                p++;
            }
            ans[i] = *s.begin();
            s.erase(ans[i]);
        }
        for(int i = 1; i < n + 1; i++)
            printf("%d%c", ans[i], i == n ? '\n' : ' ');
    }
}

猜你喜欢

转载自blog.csdn.net/head_hard/article/details/81181221