• Distinct Values(贪心)


    问题 D: Distinct Values

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 13  解决: 5
    [提交] [状态] [讨论版] [命题人:admin]

    题目描述

    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.

    输入

    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.

    输出

    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.

    样例输入

    3
    2 1
    1 2
    4 2
    1 2
    3 4
    5 2
    1 3
    2 4
    

    样例输出

    1 2
    1 2 1 2
    1 2 3 1 1
    

     思路:把一个区间的端点中值较大的一个放在前面,向后压倒!  详见代码:

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    vector<int> ans;
    priority_queue<int,vector<int>,greater<int> > store;
    int t,n,m,l,r,a[100050];
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            ans.clear();
            while(!store.empty()) store.pop();
            scanf("%d %d",&n,&m);
            for(int i=1;i<=n;i++)
            {
                a[i]=i;
                store.push(i);
            }
            while(m--)
            {
                scanf("%d %d",&l,&r);
                a[l]=max(a[l],r);
            }
            int cnt=0,now=1;
            for(int i=1;i<=n;i++)
            {
                if(a[i]<now) continue;
                while(now>a[cnt+1] && cnt+1<now)
                {
                   store.push(ans[cnt]);
                   cnt++;
                }
                while(now<=a[i])
                {
                    ans.push_back(store.top());
                    store.pop();
                    now++;
                }
            }
            for(int i=0;i<ans.size();i++)
            {
                if(i==0) printf("%d",ans[i]);
                else printf(" %d",ans[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    闭包跟装饰器
    几个常见的面试题目
    函数的进阶
    python的生成器和迭代器
    Python的数据类型
    “随手记”开发记录day09
    “随手记”开发记录day08
    “随手记”开发记录day07
    “随手记”开发记录day06
    “随手记”开发记录day05
  • 原文地址:https://www.cnblogs.com/lglh/p/9420660.html
Copyright © 2020-2023  润新知