Distinct Values(hdu 6301) (贪心+优先队列)

Distinct Values

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

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

题意:每给出区间范围[i,j],该区间没有元素相同,求满足条件且字典序最小的数组.

谈到字典序,很容易想到用贪心来做,当然得先对区间位置排序(这个很重要).区间靠前的先固定下来,用两个指针标记当前的区间范围.这其中涉及到一些细节,以及优先队列的使用(这个很关键),在纸上画画更清晰一些

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int ans[maxn];
priority_queue<int,vector<int>,greater<int> >q;
struct node
{
    int a,b;
}s[maxn];
bool cmp(node x,node y)
{
    if(x.a!=y.a) return x.a<y.a;
    else return x.b>y.b;
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++) scanf("%d%d",&s[i].a,&s[i].b);
        sort(s,s+m,cmp);
        for(int i=1;i<=n;i++) ans[i]=1;
        int L=0,R=0,mx=0;
        for(int i=0;i<m;i++)
        {
            if(s[i].b<=R) continue;
            if(s[i].a>R)
            {
                for(int k=s[i].a,j=1;k<=s[i].b;k++,j++)
                {
                    ans[k]=j;
                    mx=j;
                }
                while(!q.empty()) q.pop();//清空加在这里,而不是在else语句里
            }
            else
            {
                for(int j=L;j<s[i].a;j++) q.push(ans[j]);
                for(int j=R+1;j<=s[i].b;j++)
                {
                    if(!q.empty()) ans[j]=q.top(),q.pop();
                    else ans[j]=++mx;
                }
            }
            L=s[i].a,R=s[i].b;
        }
        for(int i=1;i<n;i++) printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值