zoukankan      html  css  js  c++  java
  • HDU6301 SET集合的应用 贪心

    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), aiaj

    holds.
    Chiaki would like to find a lexicographically minimal array which meets the facts.

    InputThere 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.
    OutputFor 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

    这个题目好就好在贪心的时候用两个指针维护,时间复杂度还是不超过O(n)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <set>
     7 
     8 #define LL long long
     9 using namespace std;
    10 
    11 int n,m;
    12 int answer[100010];
    13 struct node
    14 {
    15     int l,r;
    16 }a[100010];
    17 int cmp(node x,node y)
    18 {
    19     if(x.l==y.l)
    20         return x.r>y.r;
    21     return x.l<y.l;
    22 }
    23 set<int>t;
    24 int main()
    25 {
    26     int T;
    27     scanf("%d",&T);
    28     while(T--)
    29     {
    30         t.clear();
    31         scanf("%d%d",&n,&m);
    32         for(int i = 1; i <= n; i++){
    33             t.insert(i);
    34             answer[i] = 0;
    35         }
    36         for(int i = 0; i < m; i++)
    37         {
    38             scanf("%d%d",&a[i].l,&a[i].r);
    39         }
    40         sort(a,a+m,cmp);
    41         int l = 1,r = 1;
    42         for(int i = 0; i < m; i++)
    43         {
    44             for(int j = l; j < a[i].l; j++)
    45             {
    46                 if(answer[j]!=0)
    47                 {
    48                     t.insert(answer[j]);
    49                 }
    50                 l++;
    51             }
    52             if(r<l) r = l;
    53             for(int j = r; j <= a[i].r; j++)
    54             {
    55                 if(answer[j]==0){
    56                     answer[j] = *t.begin();
    57                     t.erase(t.begin());
    58                 }
    59                 r++;
    60             }
    61         }
    62         for(int i = 1; i < n; i++)
    63             if(answer[i]==0)
    64                 printf("1 ");
    65             else
    66                 printf("%d ",answer[i]);
    67         if(answer[n]==0)
    68             printf("1
    ");
    69         else
    70             printf("%d
    ",answer[n]);
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    已设定选项readonly请加!强制执行
    Linux下NVIDIA显卡驱动安装方法
    C#使用MiniDump导出内存快照MiniDumper
    一些陈旧的注册表垃圾清理脚本:注册表冗余数据清理.reg
    脚本精灵一些脚本
    本地安装SonarQube Community8.1社区版进行代码质量管控
    spring redistemplate中使用setHashValueSerializer的设置hash值序列化方法
    spring-core-5.0.6.RELEASE-sources.jar中java源代码不全
    lombok插件/slf4j中字符串格式化
    light4j/light-4j一个轻量级的低延时、高吞吐量、内存占用量小的API平台
  • 原文地址:https://www.cnblogs.com/--lr/p/9362607.html
Copyright © 2011-2022 走看看