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
  • 相关阅读:
    VisualSVN 4.0.11补丁原创发布
    用Advanced Installer制作DotNetBar for Windows Forms 12.0.0.1_冰河之刃重打包版详解
    树霉派4B换国内源注意事项
    StepShot4.3.0安装包_KeyGen发布
    NoSQL Manager for Cassandra 3.2.0.1 带Key
    NoSQL Manager for MongoDB 4.6.0.3 带key
    数值分析 三次样条插值及实现
    POJ 1753 Flip Game 暴力 深搜
    最短路问题 Dijkstra算法- 路径还原
    任意两点间的最短路问题 Floyd-Warshall算法
  • 原文地址:https://www.cnblogs.com/lglh/p/9420660.html
Copyright © 2011-2022 走看看