算是一道dp

首先,排序好每一个架子上都是一段区间,然后只需要统计每个点向左向右最长延伸的区间。

所以我们预处理出每个点以左、以右最大能延伸的长度(最多能选几个差值不超过k的)

然后枚举每个点作为断点,sum取max即可

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int n,k,a[N],sum_l[N],sum_r[N];
int read(){
int sum=;
char ch=getchar();
while (ch<''||ch>'')
ch=getchar();
while (ch>=''&&ch<=''){
sum=sum*+ch-'';
ch=getchar();
}
return sum;
}
int main(){
int ans=,j;
n=read();
k=read();
for (int i=;i<=n;i++)
a[i]=read();
sort(a+,a+n+);
sum_l[]=;j=;
for (int i=;i<=n;i++){
while (a[i]-a[j]>k) j++;
sum_l[i]=max(sum_l[i-],i-j+);
}
sum_r[n]=;j=n;
for (int i=n-;i>=;i--){
while (a[j]-a[i]>k) j--;
sum_r[i]=max(sum_r[i+],j-i+);
}
for (int i=;i<n;i++)
ans=max(ans,sum_l[i]+sum_r[i+]);
printf("%d",ans);
return ;
}

【洛谷P3143】Diamond Collector的更多相关文章

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

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

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

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

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

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

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

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

  5. 洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector

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

  6. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  7. 洛谷P1650赛马与codevs 2181 田忌赛马

    洛谷P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负 ...

  8. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  9. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. mysql优化要点(一)

    一  sql语句中使用函数需要注意,因为有可能结果每条语句都会调用,甚至表内每条记录都会调用. 二  注意转换,会消耗大量性能,甚至字符串的encoding不同,也会出现非常大的消耗 三  myisa ...

  2. table 固定表头

    1 .table { border-collapse: collapse; } .table th { display: table-cell; } .fixedThead {//thead disp ...

  3. Python执行系统命令的方法 os.system(),os.popen(),commands

    os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...

  4. dfs 翻棋盘end

    #include<iostream> char data[16]; int a[16]; int d[4] = { -4, 1, 4, -1 }; char b[16]; int flag ...

  5. WCF服务二:创建一个简单的WCF服务程序

    在本例中,我们将实现一个简单的计算服务,提供基本的加.减.乘.除运算,通过客户端和服务端运行在同一台机器上的不同进程实现. 一.新建WCF服务 1.新建一个空白解决方案,解决方案名称为"WC ...

  6. EBS 追前台最后一个执行的sql

    首先 从前台获取sid 然后 获取sql地址 ; 最后 获取sql文本 select * from v$sqltext_with_newlines t where t.ADDRESS = '07000 ...

  7. oracle 返回第一个不为空的列的值

    ) from emp; 作用是返回函数coalesce参数中第一个不为null的值.

  8. NetBIOS发包

    [NetBIOS发包] 1.拥有 LANA.Local Session Num即可发包. ncb_lsn,session号.指定发向哪. ncb_lana_num,lan-adapter号,指定用哪一 ...

  9. tp框架实现防止非法登录

    <?phpnamespace Admin\Controller;use Think\Controller;class UserController extends Controller {//登 ...

  10. web开发工具软件使用问题记录

    一.右键 - 添加Git Bash Here菜单 转自:http://blog.csdn.net/u014527912/article/details/51723735 步骤: 1.通过在“运行”中输 ...