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
  • 相关阅读:
    MySQL 之 数据操作
    MySQL 之 库操作,表操作
    Python之协程
    IO模式和IO多路复用
    python之线程
    Python--多线程、多进程常用概念
    计算机组成原理
    python基础-守护进程、守护线程、守护非守护并行
    win7旗舰版 安装IIS中出现的问题
    sql 2008数据事务日志已满处理方法
  • 原文地址:https://www.cnblogs.com/lglh/p/9420660.html
Copyright © 2011-2022 走看看