• Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解


    4582: [Usaco2016 Open]Diamond Collector

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 204  Solved: 136
    [Submit][Status][Discuss]

    Description

    Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare 
    time! She has collected N diamonds (N≤50,000) of varying sizes, and she wants to arrange some of th
    em in a pair of display cases in the barn.Since Bessie wants the diamonds in each of the two cases t
    o be relatively similar in size, she decides that she will not include two diamonds in the same case
     if their sizes differ by more than K (two diamonds can be displayed together in the same case if th
    eir sizes differ by exactly K). Given K, please help Bessie determine the maximum number of diamonds
     she can display in both cases together.
    给定长度为N的数列a,要求选出两个互不相交的子序列(可以不连续),满足同一个子序列中任意两个元素差的绝
    对值不超过K。最大化两个子序列长度的和并输出这个值。1 ≤ N ≤ 50000, 1 ≤ a_i ≤ 10 ^ 9, 0 ≤ K ≤ 10^ 9
     

    Input

    The first line of the input file contains N and K (0≤K≤1,000,000,000). The next NN lines each cont
    ain an integer giving the size of one of the diamonds. All sizes will be positive and will not excee
    d 1,000,000,000

    Output

    Output a single positive integer, telling the maximum number of diamonds that Bessie can showcase in
     total in both the cases.

    Sample Input

    7 3
    10
    5
    1
    12
    9
    5
    14

    Sample Output

    5

    HINT

     

    Source

      这道题当时第一次打打的是一个贪心,先去找最大的满足条件的序列,然后再去找刨去该序列后的最大的序列,虽然可以证明不对,但当时没有多想,对了70%还算比较优秀吧……
      我们可以对答案进行一下分析,首先既然对于顺序无限制我们可以将a排序,这样答案就变成了选择两个不相交的区间,将问题简化。对于答案来说,一定有两个点分别满足他是一个区间的右端点,另一个区间在他的右侧或他是一个区间的左端点,另一个区间在他右侧,所以,我们只需要把每个点向左和向右是否包含他的最大合法区间长度记录下来,然后去枚举每个点作为上述特殊点是答案是多少,直接输出最大值即可。
      
     1 #include<iostream>
     2 #include<cstdlib>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #include<algorithm>
     7 #include<cmath>
     8 #include<map>
     9 #include<vector>
    10 #define N 50005
    11 using namespace std;
    12 int n,l,a[N],f[2][N][2];
    13 int main()
    14 {
    15     scanf("%d%d",&n,&l);
    16     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    17     sort(a+1,a+n+1);
    18     int la=0;
    19     for(int i=1;i<=n;i++)
    20     {
    21         int t=la;
    22         while(a[i]-a[i-t]>l) t--;
    23         t++;
    24         la=t;
    25         f[0][i][0]=t;
    26         f[0][i][1]=max(f[0][i-1][0],f[0][i-1][1]);
    27     }
    28     la=0;
    29     for(int i=n;i>0;i--)
    30     {
    31         int t=la;
    32         while(a[i+t]-a[i]>l) t--;
    33         t++;
    34         la=t;
    35         f[1][i][0]=t;
    36         f[1][i][1]=max(f[1][i+1][0],f[1][i+1][1]);
    37     }
    38     int ans=0;
    39     for(int i=1;i<=n;i++)
    40     {
    41         ans=max(ans,max(f[1][i][0]+f[0][i][1],f[0][i][0]+f[1][i][1]));
    42     }
    43     printf("%d
    ",ans);
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    C#组合算法 (2)
    用匿名函数来更方便的使用缓存
    之通用权限(五):项目描述表组(转)
    软件代码(程序)管理办法
    搭建开发架构的思路
    (转)七秘诀工作效率与薪水翻番
    uml学习入门 2面向对象方法分析与设计
    一个.Net开发组合:Powerdesigner+数据库+分层设计+.Net代码生成器+VS2005
    Log4Net介绍
    【自然框架】之通用权限(一):简介、数据结构 (转)
  • 原文地址:https://www.cnblogs.com/liutianrui/p/7576300.html
Copyright © 2020-2023  润新知