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
  • 相关阅读:
    Linux GCC
    操作系统中的一些数据结构
    《C程序设计》指针部分学习记录-易混点
    操作系统原理与实践-试验2操作系统的引导记录
    输入字符串测试
    实模式和保护模式
    Unity周记: 2020.07.06-07.12
    Unity论坛问答-如何裁剪一个多边形
    Unity论坛问答-如何让Camera.ScreenToWroldPoint在XZ平面上移动
    Unity杂谈-SRP为什么不再使用CG
  • 原文地址:https://www.cnblogs.com/lglh/p/9420660.html
Copyright © 2011-2022 走看看