zoukankan      html  css  js  c++  java
  • hdu 6301 Distinct Values (贪心)

    Distinct Values

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


    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 (li<jr), aiajholds.
    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 (1n,m105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1lirin).

    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个给定区间li...ri的数字互不相同。
     
    贪心。
    首先处理m个给定的区间。用ri数组存储1...n对应的最大右边界,使区间数字互不相同。更新是这样的:ri[i]=max(r,ri[i])。
    然后遍历ri数组,依次处理每个区间即可。当前区间包含待处理区间时,可以直接跳过。技巧性在于要利用一个值越小优先级越高的1...n的优先队列。不断把其中的数拿出来有放回以实现字典序最小。
     
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<cmath>
    
    using namespace std;
    
    const int maxn=100000;
    
    int ri[maxn+10];
    int arr[maxn+10];
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
    
            for(int i=1;i<=n;i++)
                ri[i]=i;
            for(int i=0,a,b;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                ri[a]=max(ri[a],b);
            }
    
            priority_queue<int,vector<int>,greater<int> > q;
            for(int i=1;i<=n;i++)
                q.push(i);
    
            q.pop();arr[1]=1;
            for(int i=1,a=1,b=1;i<=n;i++)
            {
                if(ri[i]<=b)  continue;
                for(int j=a;j<i;j++)
                    q.push(arr[j]);
                for(int j=b+1;j<=ri[i];j++)
                {
                    arr[j]=q.top();
                    q.pop();
                }
                a=i;b=ri[i];
            }
    
            for(int i=1;i<=n;i++)
            {
                if(i==1)
                    printf("%d",arr[i]);
                else
                    printf(" %d",arr[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    WinAPI: DrawFrameControl 绘制控件
    WinAPI: SetPixel 和 SetPixelV 设置设备环境中指定位置的颜色
    WinAPI: GetSystemInfo 获取系统信息
    WinAPI: GetDiskFreeSpace 获取磁盘组织与容量信息
    WinAPI: GetDiskFreeSpaceEx 获取磁盘容量信息
    分享:Afinal 0.3.5 发布,Android快速开发框架
    海量数据多路归并排序的c++实现(归并时利用了败者树)
    当TransferEncoding遇上ContentEncoding_虚拟现实_百度空间
    chunked 编码 解码 c算法 yaneng的专栏 博客频道 CSDN.NET
    败者树 多路平衡归并外部排序 Dreaming.O的专栏 博客频道 CSDN.NET
  • 原文地址:https://www.cnblogs.com/acboyty/p/9683941.html
Copyright © 2011-2022 走看看