题目描述

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

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

输入输出格式

输入格式:

The first line of the input file contains NN and KK (0 \leq K \leq 1,000,000,0000≤K≤1,000,000,000).

The next NN 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,0001,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

不明白splay标签怎么来的。。

打了好长时间splay 最后弃疗了

正解二分

屠龙宝刀点击就送

#include <algorithm>
#include <cstring>
#include <cstdio>
#define N 50005
using namespace std;
int n,k,a[N],ans1[N],ans2[N];
int find_1(int x)
{
int l=,r=n;
for(int mid;l<=r;)
{
mid=(l+r)>>;
if(a[mid]<x) l=mid+;
else r=mid-;
}
return l;
}
int find_2(int x)
{
int l=,r=n;
for(int mid;l<=r;)
{
mid=(l+r)>>;
if(a[mid]>x) r=mid-;
else l=mid+;
}
return r;
}
inline int max(int a,int b){return a>b?a:b;}
int Main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i) scanf("%d",&a[i]);
sort(a+,a++n);
for(int i=;i<=n;++i)
{
ans1[i]=i-find_1(a[i]-k)+;
ans2[i]=find_2(a[i]+k)-i+;
}
int ans=;
for(int i=;i<=n;++i)
{
ans=max(ans,ans1[i-]+ans2[i]);
ans1[i]=max(ans1[i],ans1[i-]);
}
printf("%d\n",ans);
return ;
}
int sb=Main();
int main(int argc,char *argv[]){;}

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

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

  6. 【洛谷P3143】Diamond Collector

    算是一道dp 首先,排序好每一个架子上都是一段区间,然后只需要统计每个点向左向右最长延伸的区间. 所以我们预处理出每个点以左.以右最大能延伸的长度(最多能选几个差值不超过k的) 然后枚举每个点作为断点 ...

  7. 洛谷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 ...

  8. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  9. 洛谷P3145 [USACO16OPEN]分割田地Splitting the Field

    P3145 [USACO16OPEN]分割田地Splitting the Field 题目描述 Farmer John's NN cows (3 \leq N \leq 50,0003≤N≤50,00 ...

随机推荐

  1. Android如何解析json数组对象

    import org.json.JSONArray; import org.json.JSONObject; //jsonData的数据格式:[{ "id": "27Jp ...

  2. iOS ipa包重签名

    背景:公司做游戏SDK的,提供SDK给第三方后,他们打包过来我们需要分发在不同的渠道,这个时候需要修改SDK的配置文件,ipa文件修改后是需要手机越狱或者ipa重签名才能安装成功的,所以研究了一下重签 ...

  3. ASP.NET自定义控件组件开发

    ASP.NET的开发都是事件驱动的,现在我们就来为控件添加事件.在事件之前 对委托事件要要熟悉. 其实定义事件的步骤很简单: 1.声明一个委托. 2.定义一个携带事件信息的类. 3.定义事件 4.定义 ...

  4. MS SQL的CASE...WHEN...THEN...END语法

    根据多个可能的答案检查一个值或变量. 举例说明: SELECT [type],CASE [type] WHEN 'TT' THEN 'TYPE_TABLE' WHEN 'FN' THEN 'SQL_S ...

  5. python translate maketrans 字符串替换

    string1='abcd-1234' print(string1.translate(string1.maketrans('abc','ABC'))) a='aeiou' b=' string2=' ...

  6. [Xcode 实际操作]六、媒体与动画-(11)UIView视图卷曲动画的制作

    目录:[Swift]Xcode实际操作 本文将演示UIView视图卷曲动画的制作. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit cla ...

  7. java中对List进行分组和排序

    排序 对List进行排序,有两种办法 第一个是用java提供的工具类Collections提供的sort方法进行排序 废话不多说,上代码 首先定义一个Student public class Stud ...

  8. 剑指Offer的学习笔记(C#篇)-- 链表中倒数第K个点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. 一 . 数据结构基础概念普及(线性表). 线性表可分为顺序表与链表,它们是堆栈.队列.树.图等数据结构的实现基础. 顺序表,线性表的顺序存储结构是 ...

  9. js根据等号(=)前名称获取参数值

    var GetUrlParam=function (paraName) { var url = document.location.toString(); var arrObj = url.split ...

  10. UVaLive6435(dp)

    要点 题意:两个数据传输数列,每个数列里有若干个数据包并给出发出时间\(t\),每个数据包到达的时间\(T\)是\(t <= T < t + D\),问有多少种到达序列. 将题目转化为第二 ...