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. -Dmaven.test.skip=true 和 -DskipTests

    -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下. -Dmaven.test.skip=true,不执行测试用例,也不编译测试 ...

  2. open_vPGPv

    加密 // create an instance of the library PGPLib pgp = new PGPLib(); // Import the main company public ...

  3. 【转载】C#使用Math.Ceiling方法对计算结果向上取整操作

    在C#的数值运算中,有时候需要对计算结果进行向上取整操作,支持设定结算结果的有效位数,Math.Ceiling方法是C#中专门用来对数值进行向上取整的方法,此方法和Math.Round方法.Math. ...

  4. Postgres 多实例实例部署方式

    Postgres 数据库在原有示例正常运行情况下,新增一个端口示例,主要目的解决新的项目和原有项目的数据库部署不在冲突,可以独立运行,备份和还原数据互不影响,主要用的的命令有  initdb 数据库初 ...

  5. 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)

    超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...

  6. day53-python之会话

    from django.shortcuts import render,redirect # Create your views here. import datetime def login(req ...

  7. Redis数据结构和使用场景,redis内存淘汰策略

    什么样的数据适合放入Redis? sql执行耗时特别久,且结果不频繁变动的数据,适合放入Redis. Redis是单线程的,为什么会这么快? 纯内存操作 单线程操作,避免频繁的上下文切换 采用了非阻塞 ...

  8. Spring事务传播机制与隔离机制

    详情查看 https://www.jianshu.com/p/249f2cd42692

  9. Python学习日记(二十一) 异常处理

    程序中异常的类型 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基 ...

  10. Linux常用时间函数

    time()函数: NAME time - get time in seconds SYNOPSIS #include <time.h> time_t time(time_t *tloc) ...