HDU 6301 (贪心)

Distinct Values

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


 

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的区间,只保留右端点最大的那个。

对于左端点为i+1的区间,他的最大右端点只有在比i的最大右端点大的时候才会保留。

之后就用优先队列或者set模拟贪心过程即可。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,l[100005],r[100005],ans[100005];
priority_queue<int, vector<int>, greater<int>> q;
void swap(int &a,int &b){
    int t=a;
    a=b;
    b=t;
}
void qsort(int L,int R){
    int i=L,j=R,xl=l[(L+R)>>1],xr=r[(L+R)>>1];
    do{
        while(l[i]<xl||(l[i]==xl&&r[i]>xr)) i++;
        while(l[j]>xl||(l[j]==xl&&r[j]<xr)) j--;
        if(i<=j){
            swap(l[i],l[j]);
            swap(r[i],r[j]);
            i++;
            j--;
        }
    }while(i<=j);
    if(L<j) qsort(L,j);
    if(i<R) qsort(i,R);
}
int min(int a,int b){
    if(a<b) return a;
    return b;
}
int max(int a,int b){
    if(a>b) return a;
    return b;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]);
        qsort(1,m);
        while(!q.empty()) q.pop();
        for(int i=1;i<=n;i++){
            q.push(i);
            ans[i]=1;
        }
        int last=0;
        for(int i=1;i<=m;i++){
            if(last!=0&&r[last]>=r[i]) continue;
            if(last!=0){
                for(int j=l[last];j<=min(r[last],l[i]-1);j++){
                    q.push(ans[j]);
                }
                for(int j=max(r[last]+1,l[i]);j<=r[i];j++){
                    ans[j]=q.top();
                    q.pop();
                }
            }else{
                for(int j=l[i];j<=r[i];j++){
                    ans[j]=q.top();
                    q.pop();
                }
            }
            last=i;
        }
        for(int i=1;i<n;i++) printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wyj_alone_smile/article/details/81177083