HDU 6301 Distinct Values(优先队列)/(set)

Distinct Values

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


 

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

 

题意:

n个数,m个区间,要求每个区间中都不能出现相同的数字。输出字典序最小的序列。

思路:

(1)利用优先队列(从小到大排序),每次我们将区间外的小数字加入队列,并按照队列顺序插入待排列的区间。

           这个算是卡过去的把。跑了1950ms。

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int MAXN = 1e5+10;

int t,n,m;
int ans[MAXN];
struct node{
    int l,r;
}p[MAXN],pre;
struct CMP{
    bool operator ()(int a,int b){
        return a > b;
    }
};
bool cmp(node a,node b){
    if(a.l!=b.l)    return a.l < b.l;
    else return a.r > b.r;
}
int main(){
    scanf("%d",&t);
    while(t--){
        memset(ans,0,sizeof(ans));
        memset(p,0,sizeof(p));
        priority_queue<int, vector<int>,CMP>Q;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%d%d",&p[i].l,&p[i].r);
        }
        sort(p,p+m,cmp);
        pre.l = p[0].l;
        pre.r = p[0].r;
        int maxx = 0;
        for(int i=pre.l;i<=pre.r;i++){
            ans[i] = ++maxx;
        }
        for(int i=1;i<m;i++){
            if(p[i].r<=pre.r)   continue;
            else{
                for(int j=pre.l;j<min(p[i].l,pre.r+1);j++){
                    Q.push(ans[j]);
                }
                int temp =max(pre.r+1,p[i].l);
                while(!Q.empty()&&temp<=p[i].r){
                    ans[temp++]=Q.top();
                    Q.pop();
                }
                if(temp<=p[i].r){
                    for(int k=temp;k<=p[i].r;k++){
                        ans[k]=++maxx;
                    }
                }
                pre.l = p[i].l;
                pre.r = p[i].r;
            }
        }
        for(int i=1;i<n;i++){
            if(ans[i]==0)  ans[i] = 1;
            printf("%d ",ans[i]);
        }
        if(ans[n]==0)  ans[n] = 1;
        printf("%d\n",ans[n]);
    }
    return 0;
}
/*
1000
8 3
1 3
2 5
3 8
13 4
1 5
3 6
7 10
9 13
*/

 

正解:利用set的去重和排序功能,利用tot标记一下当前可入set的左界(可重复利用),pre数组记录的是区间左界。

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int MAXN =1e6+10;
const int INF = 0x3f3f3f3f;
const int N = 1010;

int n,m,t;
int pre[MAXN],ans[MAXN];

int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)   pre[i] = i;
        int l,r;
        for(int i=0;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 tot = 1;
        set<int>se;
        for(int i=1;i<=n;i++)   se.insert(i);
        for(int i=1;i<=n;i++){
            while(tot < pre[i]) {
                se.insert(ans[tot]);
                tot++;
            }
            ans[i] = *se.begin();
            se.erase(ans[i]);
        }
        for(int i=1;i<n;i++){
            printf("%d ",ans[i]);
        }printf("%d\n",ans[n]);
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值