洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述
Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected diamonds (
) of varying sizes, and she wants to arrange some of them in a pair of display cases in the barn.
Since Bessie wants the diamonds in each of the two cases to 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 (two diamonds can be displayed together in the same case if their sizes differ by exactly
). Given
, please help Bessie determine the maximum number of diamonds she can display in both cases together.
奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了N颗不同大小的钻石(N<=50,000),现在她想在谷仓的两个陈列架上摆放一些钻石。
Bessie想让这些陈列架上的钻石保持相似的大小,所以她不会把两个大小相差K以上的钻石同时放在一个陈列架上(如果两颗钻石的大小差值为K,那么它们可以同时放在一个陈列架上)。现在给出K,请你帮Bessie确定她最多一共可以放多少颗钻石在这两个陈列架上。
输入输出格式
输入格式:
The first line of the input file contains and
(
).
The next lines each contain an integer giving the size of one of the
diamonds. All sizes will be positive and will not exceed .
输出格式:
Output a single positive integer, telling the maximum number of diamonds that
Bessie can showcase in total in both the cases.
输入输出样例
7 3
10
5
1
12
9
5
14
5
从左往右,从右往左各扫一遍,记录从某个点开始最多能取的数量。
然后枚举中点往左右取,记录最大值。
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[mxn];
int n,k;
int w[mxn],r[mxn];
int main(){
n=read();k=read();
int i,j;
for(i=;i<=n;i++){
a[i]=read();
}
sort(a+,a+n+);
int hd=;
for(i=;i<=n;i++){
while(a[i]-a[hd]>k)hd++;
w[i]=max(w[i-],i-hd+);
}
hd=n;
for(i=n;i;i--){
while(a[hd]-a[i]>k)hd--;
r[i]=max(r[i+],hd-i+);
}
int ans=;
for(i=;i<n;i++){
ans=max(ans,w[i]+r[i+]);
}
printf("%d\n",ans);
return ;
}
洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector的更多相关文章
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- [USACO16OPEN]钻石收藏家Diamond Collector
由于相差不超过k才可以放在一起,要判断不超过k这个条件,显然我们需要排序 首先我们需要一个f数组,f[i]意义看代码开头注释, 假设我们可以选择的某一个区间是a[l]~a[r](已排序且最优(最长的意 ...
- 【洛谷P3143】Diamond Collector
算是一道dp 首先,排序好每一个架子上都是一段区间,然后只需要统计每个点向左向右最长延伸的区间. 所以我们预处理出每个点以左.以右最大能延伸的长度(最多能选几个差值不超过k的) 然后枚举每个点作为断点 ...
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...
- 洛谷P3145 [USACO16OPEN]分割田地Splitting the Field
P3145 [USACO16OPEN]分割田地Splitting the Field 题目描述 Farmer John's NN cows (3 \leq N \leq 50,0003≤N≤50,00 ...
随机推荐
- 人人必知的10个 jQuery 小技巧
原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧 收集的10个 jQuery ...
- Android(java)学习笔记148:网易新闻RSS客户端应用编写逻辑过程
1.我们的项目需求是编写一个新闻RSS浏览器,RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,是使用最广泛的XML应用.RSS目前广泛用于网上新闻频道,bl ...
- build.sbt的定义格式
一个简单的build.sbt文件内容如下: name := "hello" // 项目名称 organization := "xxx.xxx.xxx" // 组 ...
- "Uncaught SyntaxError: Unexpected token <"错误完美解决
今天写代码的时候发现了"Uncaught SyntaxError: Unexpected token <" <html>的js错误,而且还是html的第一行,我就 ...
- Spring-2-官网学习
spring生命周期回调 结合生命周期机制(官网提供) 1.实现InitializingBean接口重写void afterPropertiesSet() throws Exception;方法 使用 ...
- glassfish配置中数据库密码加密方法
glassfish配置中数据库密码加密方法 Glassfish中的数据库连接池需要使用密文保存数据库密码.如果不是,则可按如下方法可配置 通过Glassfish中的Alias实现,配置方法如下: 1. ...
- ios之UIProgressView
UIProgressView和UIActivityIndicator有些类似 但是不同之处在于, UIProgressView能够更加精确的反应进度 UIActivityIndicator则只能表 ...
- CF-1100 E Andrew and Taxi
CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...
- 初遇Linux
Ctrl+Alt+(F1-F6):切换虚拟终端 Ctrl+Alt:鼠标切换界面 $:普通用户登录后系统的提示符 #:root用户登录后系统的提示符 Linux命令 exit 用于退出目前的shell ...
- link与@import导入css样式区别
XML/HTML代码<link rel="stylesheet" rev="stylesheet" href="CSS文件" type ...