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): 2321    Accepted Submission(s): 748


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

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6301

    解析 先把区间排序 左端点靠左 右端点靠右 然后就可以根据右端点是否递增进行 贪心 然后就可以了。

    memset也比较耗时 所以就用多少开多少。

    AC代码

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<set>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 const int maxn=1e6+20,mod=1e9+7,inf=0x3f3f3f3f;
     8 typedef long long ll;
     9 #define pb push_back
    10 #define mp make_pair
    11 #define X first
    12 #define Y second
    13 #define all(a) (a).begin(), (a).end()
    14 #define fillchar(a, x) memset(a, x, sizeof(a))
    15 #define huan printf("
    ");
    16 #define debug(a,b) cout<<a<<" "<<b<<" ";
    17 /*================================================================================*/
    18 struct node
    19 {
    20     int l,r;
    21     bool operator <(const node &a)const
    22     {
    23         if(l==a.l)
    24             return r>a.r;
    25         return l<a.l;
    26     }
    27 }a[maxn];
    28 //int ans[maxn];
    29 int main()
    30 {
    31     int n,m,t;
    32     while(~scanf("%d",&t))
    33     {
    34         while(t--)
    35         {
    36             scanf("%d%d",&n,&m);
    37             vector<int> ans(n+1,1);
    38            //fillchar(ans,0);
    39             for(int i=0;i<m;i++)
    40                 scanf("%d%d",&a[i].l,&a[i].r);
    41             sort(a,a+m);
    42             set<int> s;
    43             for(int i=1;i<=n;i++) s.insert(i);
    44             int l=1,r=0,flag=0;
    45             for(int i=0;i<m;i++)
    46             {
    47                 if(a[i].r<r)           //右端点小于r就不用管了 
    48                     continue;
    49                 else
    50                 {
    51                     if(i!=0)
    52                     {
    53 
    54                         for(int k=a[flag].l;k<=min(a[i].l-1,r);k++) //将之前用过的数 且不在当前区间的数加入set
    55                             s.insert(ans[k]);
    56                         l=max(r+1,a[i].l);
    57                     }
    58                     else                        //更新l r
    59                         l=a[i].l;
    60                     r=a[i].r;                                      
    61                     flag=i;   //上一个进行贪心的区间
    62                 }
    63                 for(int j=l;j<=r;j++)
    64                 {
    65                     ans[j]=*s.begin();s.erase(ans[j]); //把用过的从set删除
    66                 }
    67             }
    68             for(int i=1;i<=n;i++)
    69             {
    70                 //if(!ans[i])ans[i]=1;
    71                 if(i!=1)
    72                     printf(" %d",ans[i]);
    73                 else
    74                     printf("%d",ans[i]);
    75             }
    76             printf("
    ");
    77         }
    78     }
    79 }
  • 相关阅读:
    单元测试之Stub和Mock
    【爆牙齿】说说我期待中的苹果那块板子。
    【爆牙游记】黄山归来不看岳-翻山。
    【爆牙齿】微软的坟墓:Windows 7。(四)
    【爆牙齿】微软的坟墓:Windows 7。(二)
    【爆牙齿】微软的坟墓:Windows 7。(三)
    【爆牙齿】微软的坟墓:Windows 7。(一)
    【爆牙齿】微软的坟墓:Windows 7。(五)
    【爆牙游记】黄山归来不看岳-日出。
    过年照片集。
  • 原文地址:https://www.cnblogs.com/stranger-/p/9359965.html
Copyright © 2011-2022 走看看