HDU 6301 Distinct Values 思维模拟+优先队列

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

Sample Output

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

1 2
1 2 1 2
1 2 3 1 1

题意:给出长度为n的数组的m个区间,要求区间内的数字各不相同,数组长为n,按照字典序排列且数字都为正数

思路:要求数组都为正数所以先令数组整体为1,然后开始判断,如果区间不相交的话,从区间左侧到右侧直接从1开始填写,如果相交的话,则将2个相交区间的第一个区间的不相交部分取出,用优先队列存储,放入相交区间的第二个区间,如果足够的话,填入后清空,不足够的话,找区间相交部分的最大值,然后从最大值+1依次填写

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <functional>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;
const int MAXN=1e5+5;
typedef long long ll;
struct node
{
    int l;
    int r;
}a[MAXN];
int ans[MAXN];
priority_queue<int,vector<int>,greater<int> >q;
bool cmp(node a,node b)
{
    if(a.l!=b.l)
        return a.l<b.l;
    else
        return a.r<b.r;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
            scanf("%d%d",&a[i].l,&a[i].r);
        sort(a,a+m,cmp);
        for(int i=1;i<=n;i++)
            ans[i]=1;
        int l=0,r=0;
        int maxx=0;
        for(int i=0;i<m;i++){
             if(a[i].r<=r)
                continue;
             if(a[i].l>r){   //完全不相交
                for(int j=a[i].l,k=1;j<=a[i].r;j++,k++){
                    ans[j]=k;
                    maxx=k;
                }
                l=a[i].l;
                r=a[i].r;
                while(!q.empty())
                    q.pop();
             }
             else {            //相交
                for(int j=l;j<a[i].l;j++){  //不相同的部分压入队列中
                    q.push(ans[j]);
                }
                for(int j=r+1;j<=a[i].r;j++){
                    if(!q.empty()){
                        ans[j]=q.top();
                        q.pop();
                    }
                    else
                        ans[j]=++maxx;
                }
                l=a[i].l;
                r=a[i].r;
             }
        }
        for(int i=1;i<n;i++)
            printf("%d ",ans[i]);
         printf("%d\n",ans[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81225966