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

输入

The first line of the input file contains N and K (0≤K≤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.

样例输入

7 3
10
5
1
12
9
5
14

样例输出

5
分析:取两个盒子,每个盒子里的数的极差不超过k,问两个盒子里的数最大的和是多少;
   排序,预处理每个数能达到的最大长度;
   从后向前更新最大值,注意每次更新完之后,可以把当前数达到的最大长度更新;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,a[maxn],p[maxn],ma;
int main()
{
int i,j,k,t;
scanf("%d%d",&n,&k);
rep(i,,n-)scanf("%d",&a[i]);
sort(a,a+n);
rep(i,,n-)
{
int l=i,r=n-,ans=i;
while(l<=r)
{
int mid=l+r>>;
if(a[mid]<=a[i]+k)ans=mid,l=mid+;
else r=mid-;
}
p[i]=ans-i+;
}
for(i=n-;i>=;i--)
{
ma=max(ma,p[i]+p[i+p[i]]);
p[i]=max(p[i],p[i+]);
}
printf("%d\n",ma);
//system ("pause");
return ;
}
 

Diamond Collector的更多相关文章

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

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

  2. Diamond Collector (动态规划)

    问题 I: Diamond Collector 时间限制: 1 Sec  内存限制: 64 MB提交: 22  解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, alwa ...

  3. Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

    4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: ...

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

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

  5. bzoj4582[Usaco2016 Open]Diamond Collector

    bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装 ...

  6. 【BZOJ 4582】【Usaco2016 Open】Diamond Collector

    http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...

  7. BZOJ 4582: [Usaco2016 Open]Diamond Collector

    Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...

  8. 【洛谷P3143】Diamond Collector

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

  9. [USACO16OPEN]钻石收藏家Diamond Collector

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

随机推荐

  1. 10682 deathgod想知道的事(数论)

    10682 deathgod想知道的事 该题有题解 时间限制:1000MS  内存限制:65535K提交次数:265 通过次数:14 题型: 编程题   语言: G++;GCC Description ...

  2. 10- python 网络爬虫分析

    Python 网络爬虫简单分析 import urllib2 response = urllib2.urlopen("http://www.baidu.com") print re ...

  3. iOS tableView的系统分割线定格设置以及分割线自定制

    一.关于分割线的位置. 分割线的位置就是指分割线相对于tableViewCell.如果我们要根据要求调节其位置,那么在iOS7.0版本以后,提供了一个方法如下: if ([self.tableView ...

  4. Chapter 1 First Sight——26

    "Which ones are the Cullens?" I asked. "They don't look related…" 哪一个是卡伦,我问道,他们都 ...

  5. shell之路【第四篇】输入输出重定向

    输出重定向 命令输出重定向的语法为: command > file 或 command >> file 这样,输出到显示器的内容就可以被重定向到文件.果不希望文件内容被覆盖,可以使用 ...

  6. mac中使用终端生成RSA私钥和公钥文件

    1.打开终端输入:cd Desktop/   //进入桌面 2.OpenSSL   //打开 OpenSSL 3.生成私钥pem,  执行命令   genrsa -out rsa_private_ke ...

  7. grub修复

    sudo mount /dev/sda1 /boot/efi sudo modprobe efivarfs sudo grub-install /dev/sda sudo update-grub su ...

  8. 文件描述符和exec() close_on_exec

    #include <fcntl.h> #include <iostream> #include <unistd.h> using namespace std; in ...

  9. oracle 常用sql语句

    oracle 常用sql语句 1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_sizefrom d ...

  10. Swift 使用Extension 场景 浅析

    别人一看到我的 Swift 代码,立刻就会问我为什么如此频繁的使用 extension.这是前几天在我写的另一篇文章中收到的评论: 我大量使用 extension 的主要目的是为了提高代码可读性.以下 ...