zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Spring中Bean及@Bean的理解
    Visual Studio(VS)秘钥集合
    PLC不能初始化问题
    【原创】C# API 未能创建 SSL/TLS 安全通道 问题解决
    【原创】XAF CriteriaOperator 使用方式汇总
    【原创】.Net WebForm Calendar 日历控件常用方法
    【原创】XAF常用属性字段设置
    【记录】Windows 操作系统常用快捷命令
    【XAF】常见错误以及对应解决方法
    【原创】XAF ITreeNode+NonPersistent 使用方式
  • 原文地址:https://www.cnblogs.com/lglh/p/9420660.html
Copyright © 2011-2022 走看看