HDU 6301 Distinct Values 【贪心 + 优先队列】

11 篇文章 0 订阅
2 篇文章 0 订阅

2018 Multi-University Training Contest 1 1004

Distinct Values

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

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


题意:需要输出 n 个数的一个数列,题目给出 m 个区间,要求数列满足区间内的数不重复,并且要求整个数列的字典序最小

比如第三个样例: 5 2   [1,3] [2,4]

首先安排 1-3 的区间,字典序最小且不重复,当然是 1 2 3了

然后安排 2-4 的区间,2 3 号位已经放了 2 3 了,第四个位置只要不为 2 和 3 即可,那么最放 1,最后没限定,可以放 1

所以答案为 1 2 3 1 1

这道题其实是个贪心的策略,因为如果一个区间被另一个区间包含,那么这个被包含的区间就完全不需要被考虑,为了字典序最小,所以我们应该先处理区间考前的,即起点考前,终点靠后的优先,被包括的忽略,相交的做标记,然后从标记点往后处理才不会TLE,重点是前一个区间与后一个区间不重合的部分,那部分的数字需要回收重复利用才能使字典序最小,我们可以用一个队列存储可以使用的数字,但是每次都把数字排序的话时间太长了,所以使用优先队列,一个小根堆解决问题。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define mem(a,x) memset(a,x,sizeof(a))

priority_queue<int, vector<int>, greater<int> > q;

struct node{
    int l,r;
}a[100005];

bool cmp(node a,node b){
    if(a.l == b.l){
        return a.r < b.r;
    }
    return a.l < b.l;
}

int ans[100005];

int main(){
    int T,n,m;
    scanf("%d",&T);
    while(T--){
        while(!q.empty()){
            q.pop();
        }
        mem(ans,-1);
        scanf("%d %d",&n,&m);
        for(int i = 1;i <= n;i++){
            q.push(i);
        }
        for(int i = 1;i <= m;i++){
            scanf("%d %d",&a[i].l,&a[i].r);
        }
        sort(a + 1,a + 1 + m,cmp);
        int ll = 1,rr = 1;
        for(int i = 1;i <= m;i++){
            if(a[i].l != a[i + 1].l || i == m){
                for(int j = ll;j < a[i].l;j++){
                    if(ans[j] == -1){
                        ans[j] = 1;
                    }else{
                        q.push(ans[j]);
                    }
                }
                for(int j = rr;j <= a[i].r;j++){
                    if(ans[j] == -1){
                        ans[j] = q.top();
                        q.pop();
                    }
                }
                ll = a[i].l;
                rr = max(rr,a[i].r);
            }
        }
        for (int i = 1; i <= n; i++) {
            if (ans[i] == -1) {
                ans[i] = 1;
            }
        }
        for(int i = 1;i <= n;i++){
            if(i == 1){
                if(ans[i] == -1){
                    printf("1");
                }else{
                    printf("%d",ans[i]);
                }
            }else{
                if(ans[i] == -1){
                    printf(" 1");
                }else{
                    printf(" %d",ans[i]);
                }
            }
        }
        printf("\n");
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值