hdu6301 Distinct Values(优先队列)

4 篇文章 0 订阅

Distinct Values

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


 

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

解题思路

用小根堆维护可以放入区间中的数。先初始化序列全为1,并把1-n存入优先队列中。将区间排序,l小的在前面,如果l一样,则r大的在前面。对于排序后的区间,将第一组对应的区间按照从左到右的顺序赋值堆顶元素,出队。用pre表示上一组区间,初始化为第一组区间。因为堆是维护可以放入区间的数,所以,把没有和上一组重合的部分重新入队。再一个个出队赋值。要注意的是,只要处理r大于pre.r的区间,小于等于的和前面的区间完全重合了,没有处理的必要。处理完一个区间后,让pre等于该区间。

代码如下

#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#define maxn 100005
using namespace std;
struct T{
    int l, r;
    T(int l, int r): l(l), r(r){    }
    bool operator<(const T& a)const{
        if(l != a.l)
            return l < a.l;
        else
            return r > a.r;
    }
};
vector<T> vec;
int ans[maxn];
int main()
{
    int t;
    cin >> t;
    while(t --){
        int n, m;
        scanf("%d%d", &n, &m);
        priority_queue<int, vector<int>, greater<int>> que;
        for(int i = 1; i <= n; i ++){
            ans[i] = 1;
            que.push(i);
        }        
        for(int i = 1; i <= m; i ++){
            int l, r;
            scanf("%d%d", &l, &r);
            vec.push_back(T(l, r));
        }
        sort(vec.begin(), vec.end());
        T pre = vec[0];   //上一个处理的区间 
        for(int i = vec[0].l; i <= vec[0].r; i ++){
            ans[i] = que.top();
            que.pop();
        }
        for(int i = 1; i < vec.size(); i ++){
            if(vec[i].r > pre.r){
                for(int j = pre.l; j < min(vec[i].l, pre.r + 1); j ++)
                    que.push(ans[j]);
                for(int j = max(vec[i].l, pre.r + 1); j <= vec[i].r; j ++){
                    ans[j] = que.top();
                    que.pop();
                }
                pre = vec[i];  //更新pre 
            }
        }                
        for(int i = 1; i <= n; i ++){
            if(i != 1)
                putchar(' ');
            printf("%d", ans[i]);
        }
        putchar('\n');
        vec.clear();
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值