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. Win10《芒果TV》商店版更新v3.2.2:新增对Win10产品专用会员兑换码支持,全新的最具价值用户纪念奖励

    在WinHEC2016宣布Win10和ARM历史性结合之际,<芒果TV>UWP版迅速更新v3.2.2版,主要是新增对Win10产品专用会员兑换码支持,推出全新的最具价值用户纪念奖励(具有唯 ...

  2. C#匿名方法返回值赋值给变量

    The problem here is that you've defined an anonymous method which returns a string but are trying to ...

  3. 微信小程序把玩(十五)checkbox组件

    原文:微信小程序把玩(十五)checkbox组件 不得不吐糟下checkbox默认样式真是有点略丑!!!checkbox组件为一个多选框被放到checkbox-group组中,并在checkbox-g ...

  4. 仿win7窗体自动顶部最大化左侧右侧半屏效果(改写nativeEvent,使用AdjustWindowRectEx)

    #include "HMainWindow.h" #include <QApplication> #ifdef Q_OS_WIN #include <qt_win ...

  5. 通过内核对象在服务程序和桌面程序之间通信的小问题 good

    关于在通过 事件对象 在服务程序和普通桌面应用程序相互之间通信的问题,分类情况进行讨论:1.普通桌面应用程序中创建事件,服务程序中打开事件 XP的情况普通桌面应用程序中创建: m_hEvent = : ...

  6. 【Web前端Talk】React-loadable 进行代码分割的基本使用

    随着项目功能的扩充.版本迭代,我们与Webpack捆绑起来的的项目越来越大,大到开始影响加载速度了.这时我们就该考虑如何对代码进行拆分了. 这次我们一起学习一下如何对React项目中的代码进行Code ...

  7. 写在Logg SAP项目上线之际

    根据大环境大行业的惯用做法,公司建立Logg品牌是在意料之中.毫无意外的,Logg也要上到SAP系统中. 其实按它的业务模式来说上SAP系统并不困难,早在几年前就已经有做过了.无非就是接单不生产,外包 ...

  8. Python自学day-9

    一.paramiko模块 (第三方模块,需安装依赖包) paramiko模块基于SSH来连接远程主机并进行相关操作. SSHClient:用于连接远程主机并执行基本命令. import paramik ...

  9. Spring ——表达式语言 Spring Expression Language (转载)

    目录 SpEL简介与功能特性 一.为什么需要Spring表达式语言 二.SpEL表达式Hello World! 三.SpEL表达式 3.1.文字表达式 3.2.SPEL语言特性 3.2.1.属性 3. ...

  10. Appcan 自定义星星评价

    注意要先有图片:     放在 css/icons/下 HTML代码: <div class="ub ub-f1"> <input id="bz-0&q ...