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排序,这样答案就变成了选择两个不相交的区间,将问题简化。对于答案来说,一定有两个点分别满足他是一个区间的右端点,另一个区间在他的右侧或他是一个区间的左端点,另一个区间在他右侧,所以,我们只需要把每个点向左和向右是否包含他的最大合法区间长度记录下来,然后去枚举每个点作为上述特殊点是答案是多少,直接输出最大值即可。
  

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#define N 50005
using namespace std;
int n,l,a[N],f[][N][];
int main()
{
scanf("%d%d",&n,&l);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
int la=;
for(int i=;i<=n;i++)
{
int t=la;
while(a[i]-a[i-t]>l) t--;
t++;
la=t;
f[][i][]=t;
f[][i][]=max(f[][i-][],f[][i-][]);
}
la=;
for(int i=n;i>;i--)
{
int t=la;
while(a[i+t]-a[i]>l) t--;
t++;
la=t;
f[][i][]=t;
f[][i][]=max(f[][i+][],f[][i+][]);
}
int ans=;
for(int i=;i<=n;i++)
{
ans=max(ans,max(f[][i][]+f[][i][],f[][i][]+f[][i][]));
}
printf("%d\n",ans);
return ;
}

Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解的更多相关文章

  1. BZOJ 4582 [Usaco2016 Open]Diamond Collector:贪心【相差不超过k】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4582 题意: 给你n个数. 让你将其中的一些数放入两个不同的集合中,并保证同一集合内两两元 ...

  2. BZOJ 4582: [Usaco2016 Open]Diamond Collector

    Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...

  3. bzoj4582[Usaco2016 Open]Diamond Collector

    bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装 ...

  4. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  5. 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

        解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...

  6. [Usaco2016 Open]Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  7. BZOJ 4576: [Usaco2016 Open]262144

    Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...

  8. Diamond Collector

    Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...

  9. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

随机推荐

  1. QT的进程与线程(cmd /c的写法)

    该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处! 导语 在前面的几节内容中讲解了Qt网络编程的一些基本内容,这一节来看一下在Qt中进程和线程的基本应 ...

  2. Xdite:永葆热情的上瘾式学习法(套路王:每天总结自己,反省自己的作息规律,找到自己的幸运时间、幸运方法,倒霉时间、倒霉方法。幸运是与注意力挂钩的。重复才能让自己登峰造极,主动去掉运气部分来训练自己。游戏吸引自己的几个原因非常适合训练自己)good

    版权声明 本文首发自微信公共帐号: 学习学习再学习(xiaolai-xuexi) 无需授权即可转载, 甚至无需保留以上版权声明: 转载时请务必注明作者. 以下是<共同成长社区>第 58 次 ...

  3. Android零基础入门第28节:轻松掌握RelativeLayout相对布局

    原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...

  4. 毕设(五)ListView

    ListView 控件可使用四种不同视图显示项目.通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本. 可使用 ListView 控件将称作 ListItem 对象的列表条目组织成 ...

  5. BuildWinRTL.dproj 用这个重新编译就行

    BuildWinRTL.dproj 用这个重新编译就行 我每次安装新版本,都删掉了这两个函数 {$IFDEF DEBUG}exports  dbkFCallWrapperAddr,{$IF defin ...

  6. Delphi子类调用祖父类的虚函数

    因为看Delphi书的时候,就产生了疑惑.老讲调用父类虚函数,但是万一需要调用祖父虚函数怎么办?后来又经常在C++里看到,就更疑惑了 type TA = class procedure ShowMsg ...

  7. Codility---BinaryGap

    Task description A binary gap within a positive integer N is any maximal sequence of consecutive zer ...

  8. win7、win8客户端 连接win2003上的sqlserver2005 速度超慢

    解决办法:开始运行,执行netsh interface tcp set global rss=disabled autotuninglevel=disabled.关闭TCP/IP协议的自动调谐功能.

  9. Java动态规划

    1. 介绍 动态规划典型的被用于优化递归算法,因为它们倾向于以指数的方式进行扩展.动态规划主要思想是将复杂问题(带有许多递归调用)分解为更小的子问题,然后将它们保存到内存中,这样我们就不必在每次使用它 ...

  10. Binary classification - 聊聊评价指标的那些事儿【回忆篇】

    在解决分类问题的时候,可以选择的评价指标简直不要太多.但基本可以分成两2大类,我们今分别来说道说道 基于一个概率阈值判断在该阈值下预测的准确率 衡量模型整体表现(在各个阈值下)的评价指标 在说指标之前 ...