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;
}

最新文章

  1. Android原生(Native)C开发之一:环境搭建篇
  2. Fedora22编译Qt3.3.X
  3. JavaWeb学习笔记——SAX解析
  4. [转]MongoDB学习 C#驱动操作MongoDB
  5. Clean Code &ndash; Chapter 3: Functions
  6. mybatis入门-第一个程序
  7. bootstrap导航条+模态对话框+分页样式
  8. 关于git常见的一些问题
  9. BZOJ 2809: [Apio2012]dispatching [斜堆]
  10. 加密流量分析cisco
  11. Android自定义View前传-View的三大流程-Measure
  12. 设计模式之适配器模式(Adapter)(6)
  13. Win10下Clion配置opencv3
  14. ListView and gridview常用属性
  15. 关于easyUI分页
  16. ROS計算圖級(通訊架構)
  17. Linux 下安装 Python3
  18. Java基础之一:Java开发环境配置
  19. 关闭linux的防火墙
  20. SVM支持向量机总结

热门文章

  1. 最简单的centos上安装Nginx办法
  2. [LOJ6433] [PKUSC2018] 最大前缀和
  3. 2.6_Database Interface JDBC及驱动类型
  4. C# CheckBoxList绑定值,设置及获取
  5. js将文字填充与canvas画布再转为图片
  6. Sbase数据库自动截断日志
  7. 整理:史上最简单的 MySQL 教程
  8. Spring Boot 笔记 (1) - Maven、基本配置、Profile的使用
  9. phpmyadmin教程
  10. mqtt服务搭建(emqx,原emq)