zoukankan      html  css  js  c++  java
  • hdu 6301

    Distinct Values

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3105    Accepted Submission(s): 1000


    Problem Description
    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 (li<jr), aiajholds.
    Chiaki would like to find a lexicographically minimal array which meets the facts.
     
    Input
    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 (1n,m105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1lirin).

    It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.
     
    Output
    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.
     
    Sample Input
    3 2 1 1 2 4 2 1 2 3 4 5 2 1 3 2 4
     
    Sample Output
    1 2 1 2 1 2 1 2 3 1 1
     
    Source
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <vector>
     6 #include <utility>
     7 #include <queue>
     8 #include <set>
     9 using namespace std;
    10 typedef long long ll;
    11 const int N=1e5+9;
    12 int num[N],n,m,pre[N];
    13 int t;
    14 set<int>s;
    15 int main()
    16 {
    17     scanf("%d",&t);
    18     int l,r;
    19     //pre[i]: 在i位置放数时必须要考虑到的最左的位置
    20     while(t--)
    21     {   s.clear();
    22         scanf("%d%d",&n,&m);
    23         memset(num,0,sizeof(num));
    24         for(int i=1;i<=n;i++) 
    25         {  pre[i]=i;
    26            s.insert(i);
    27         }
    28         for(int i=0;i<m;i++)
    29         {
    30             scanf("%d%d",&l,&r);
    31             pre[r]=min(pre[r],l);
    32         }
    33         for(int i=n-1;i>=1;i--){//pre[i]<=pre[i+1]
    34             pre[i]=min(pre[i],pre[i+1]);//如:pre[i+1]=1,pre[i]=2,那么pre[i]一定=1
    35         }
    36         int begin=1;
    37         for(int i=1;i<=n;i++){
    38             while(begin<pre[i]){//可以和begin重复
    39                 s.insert(num[begin]);//插入新的
    40                 begin++;//同时位置不断的右移
    41             }
    42                 num[i]=*s.begin();//最小的(s里面都是满足题意的)
    43                 s.erase(num[i]);//避免重复。
    44         }
    45         for(int i=1;i<=n;i++){
    46                printf("%d%c",num[i],i==n?'
    ':' ');
    47         }
    48     }
    49     return 0;
    50 }
     
  • 相关阅读:
    24.最优布线问题(kruskal算法)
    24.最优布线问题(kruskal算法)
    Algs4-1.4.11为StaticSETofInts添加一个实列方法howMany()
    Algs4-1.4.9预测程序运行时间
    Algs4-1.4.10二分查找找出元素所在的最小索引
    Algs4-1.4.7统计算术运算与比较次数
    Algs4-1.4.8计算输入文件中相等的整数对的数量
    Algs4-1.4.6给出以下代码段的运行时间的增长数量级
    Algs4- 1.4.4参照表1.4.4为TwoSum建立一和类似的表格
    Algs4-1.4.2修改ThreeSum防止两个int值相加可能溢出
  • 原文地址:https://www.cnblogs.com/tingtin/p/9363505.html
Copyright © 2011-2022 走看看