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 NN diamonds (N \leq 50,000N≤50,000) 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 KK (two diamonds can be displayed together in the same case if their sizes differ by exactly KK). Given KK, please help Bessie determine the maximum number of diamonds she can display in both cases together.

奶牛Bessie很喜欢闪亮亮的东西(BalingBaling),所以她喜欢在她的空余时间开采钻石!她现在已经收集了N颗不同大小的钻石(N<=50,000),现在她想在谷仓的两个陈列架上摆放一些钻石。

Bessie想让这些陈列架上的钻石保持相似的大小,所以她不会把两个大小相差K以上的钻石同时放在一个陈列架上(如果两颗钻石的大小差值为K,那么它们可以同时放在一个陈列架上)。现在给出K,请你帮Bessie确定她最多一共可以放多少颗钻石在这两个陈列架上。

输入格式

The first line of the input file contains \(N\) and \(K\) \((0 \leq K \leq 1,000,000,000)\).

The next \(N\) lines each contain an integer giving the size of one of the

diamonds. All sizes will be positive and will not exceed \(1,000,000,000\).

输出格式

Output a single positive integer, telling the maximum number of diamonds that

Bessie can showcase in total in both the cases.

输入输出样例

输入 #1

7 3

10

5

1

12

9

5

14

输出 #1

5

【思路】

排序 + 双指针

【前缀思想】

很有意思的一道题目

这个需要找两组差不超过k的并且和最大的序列

因为需要差不超过k,

所以可以先将序列排一下序

然后就成为了有序的序列(很显然对吧)

这样就可以用双指针记录头和尾

然后只要头和尾的差小于等于k

就可以尝试一下这个区间能不能再扩大

所以可以很简单的就用双指针找出最长的差小于等于k的序列

【中心思想】

但是,这道题目有两个陈列架

所以找出一个最大的是不行的

而且找出次大的和最大的加起来也是不可行的

因为不保证最大和次大的区间不重叠

所以可以正序扫一遍然后找出

到每一个点之前最长的差小于等于k的序列

然后再倒序找一遍找出

到每一个点之后最长的差小于等于k的序列

【小细节】

这样,只需要再枚举一遍每一个点

然后比较前后加起来的和最大的就是了

不过,如果出现了前后最大的刚好重叠在枚举到的这个点

那么加起来的和就会重复一次

而且是不合法的

毕竟你不能把一个宝石放在两个陈列架上

所以就有了后面在比较时候的:

M = max(M,qian[i] + hou[i + 1]);

这样就不会出现一个点刚好是两个区间的交点的情况了

【完整代码】

#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std;
const int Max = 50005;
int a[Max];
int qian[Max];
int hou[Max];
int main()
{
int n,k;
cin >> n >> k;
for(register int i = 1;i <= n;++ i)
cin >> a[i];
sort(a + 1,a + 1 + n);
for(register int l = 1,r = 1;r <= n;++ r)
{
while(a[r] - a[l] > k && l <= r)l ++;
qian[r] = max(qian[r - 1],r - l + 1);
}
for(register int r = n,l = n;l >= 1;l --)
{
while(a[r] - a[l] > k && l <= r)r --;
hou[l] = max(hou[l] + 1,r - l + 1);
}
int M = 0;
for(register int i = 1;i < n;++ i)
M = max(M,qian[i] + hou[i + 1]);
cout << M << endl;
return 0;
}

洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解的更多相关文章

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

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

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

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

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

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

  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. [USACO16OPEN]钻石收藏家Diamond Collector

    由于相差不超过k才可以放在一起,要判断不超过k这个条件,显然我们需要排序 首先我们需要一个f数组,f[i]意义看代码开头注释, 假设我们可以选择的某一个区间是a[l]~a[r](已排序且最优(最长的意 ...

  6. 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)

    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...

  7. 洛谷P3387 【模板】缩点 题解

    背景 今天\(loj\)挂了,于是就有了闲情雅致来刷\(luogu\) 题面 洛谷P3387 [模板]缩点传送门 题意 给定一个\(n\)个点\(m\)条边有向图,每个点有一个权值,求一条路径,使路径 ...

  8. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  9. [洛谷P1029]最大公约数与最小公倍数问题 题解(辗转相除法求GCD)

    [洛谷P1029]最大公约数与最小公倍数问题 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P, ...

随机推荐

  1. TypeScript之接口

    1.写法 // 属性 interface Person { name:string; age:number; hobby: string; } // 函数 interface  { todo(para ...

  2. java之hibernate之关联映射之多对一单向关联

    1.在之前学习了单表的crud操作.在实际应用中,大都是多表关联操作,这篇会学习如何处理多表之间的关系. 2.考察书籍表和书籍分类表的关系.书籍表和书籍分类表之间是多对一的关系.数据库的表设计为: 3 ...

  3. 前端开发 Angular

    https://www.angularjs.net.cn/tutorial/18.html

  4. 解决cxf+springmvc发布的webservice,缺少types,portType和message标签的问题

    用cxf+spring发布了webservice,发现生成的wsdl的types,message和portType都以import的方式导入的.. 原因:命名空间问题 我想要生成的wsdl在同个文件中 ...

  5. UI5-技术篇-Hybrid App-3-Fiori 百度地图应用

    上一次在Jsbin中测试了百度地图应用:UI5-技术篇-Hybrid App-3-jsbin百度地图 ,主要思路:1.加载百度API  2.自定义控件  3.div标签加载地图,本文主要将相关实施过程 ...

  6. linux设置网卡速率

    ethtool # ethtool ethX //查询ethX网口基本设置 # ethtool –h //显示ethtool的命令帮助(help) # ethtool –i ethX //查询ethX ...

  7. dockerfile命令说明及使用

    执行Dockerfile命令: docker build -f /path/to/a/Dockerfil Dockerfile的基本结构 Dockerfile 一般分为四部分:基础镜像信息.维护者信息 ...

  8. XPath知识点【一】

    什么是 XPath? XPath 使用路径表达式在 XML 文档中进行导航 XPath 包含一个标准函数库 XPath 是 XSLT 中的主要元素 XPath 是一个 W3C 标准 XPath 路径表 ...

  9. ccze - A robust log colorizer(强大的日志着色器)

    这些程序遵循通常的GNU命令行语法,长选项以两个破折号(` - ')开头.选项摘要如下. -a, - argument PLUGIN = ARGUMENTS              使用此选项将AR ...

  10. 【2019牛客多校第一场】XOR

    题意: 给你一个集合A,里边有n个正整数,对于所有A的.满足集合内元素异或和为0的子集S,问你∑|S| n<=1e5,元素<=1e18 首先可以转化问题,不求∑|S|,而是求每个元素属于子 ...