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
  • 相关阅读:
    span与a元素的键盘聚焦性以及键盘点击性研究——张鑫旭
    小tip: 某简单的字符重叠与图形生成----张鑫旭
    HTML5 number类型文本框step属性的验证机制——张鑫旭
    小tip:FireFox下文本框/域百分比padding bug解决——张鑫旭
    视区相关单位vw, vh..简介以及可实际应用场景——张鑫旭
    好吧,CSS3 3D transform变换,不过如此!——张鑫旭
    HttpWebRequest.Proxy属性
    js动态函数
    js && ||
    eclipse工具栏sdk和avd图标
  • 原文地址:https://www.cnblogs.com/--lr/p/9362607.html
Copyright © 2011-2022 走看看