问题 I: Diamond Collector

时间限制: 1 Sec  内存限制: 64 MB
提交: 22  解决: 7
[提交][状态][讨论版]

题目描述

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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=;
int n,dp[N],len,g[N];
ll k,a[N];
int w[][];
int flag=;
int vis[]={};
string str[],ch;
int maxn=;
map<string,int>p,pp; int main() {
memset(dp,,sizeof(dp));
cin>>n>>k;
for(int i=;i<=n;i++)cin>>a[i];
sort(a+,a+n+);
int m=,r=;
for(int i=;i<=n;i++)
{
while(a[r+]-a[i]<=k&&r<n) r++;
dp[i]=r-i+;
}
for(int i=n;i>=;i--)
g[i]=max(g[i+],dp[i]);
int ans=;
for(int i=;i<=n;i++)
ans=max(ans,dp[i]+g[i+dp[i]]);
cout<<ans<<endl;
return ;
}

Diamond Collector (动态规划)的更多相关文章

  1. Diamond Collector

    Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...

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

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

  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. hdu2602Bone Collector ——动态规划(0/1背包问题)

    Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...

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

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

  8. BZOJ 4582: [Usaco2016 Open]Diamond Collector

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

  9. 【洛谷P3143】Diamond Collector

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

随机推荐

  1. [bzoj4860] [BeiJing2017]树的难题

    Description 给你一棵 n 个点的无根树.树上的每条边具有颜色. 一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci.对于一条树上的简单路径,路径上经过的所有边按顺序组成一 ...

  2. [学习笔记]Tarjan&&欧拉回路

    本篇并不适合初学者阅读. SCC: 1.Tarjan缩点:x回溯前,dfn[x]==low[x]则缩点. 注意: ①sta,in[]标记. ②缩点之后连边可能有重边. 2.应用: SCC应用范围还是很 ...

  3. HDU3829:Cat VS Dog(最大独立集)

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total ...

  4. expect 实现本地到远程的scp

    expect文件demo 令文件名为test.exp #!/usr/bin/expect -f set timeout -1 set pwd " set src_file [lindex $ ...

  5. 串的模式匹配算法(求子串位置的定位函数Index(S,T,pos))

    串的模式匹配的一般方法如算法4.5(在bo4-1.cpp 中)所示:由主串S 的第pos 个字 符起,检验是否存在子串T.首先令i 等于 pos(i 为S 中当前待比较字符的位序),j 等于 1(j ...

  6. 使用Idea远程部署调试tomcat

    转自:http://blog.csdn.net/jane1229/article/details/52402119 远程服务器的配置: 1.在远程服务器安装jdk和tomcat 2.配置环境变量 PA ...

  7. 转:LVS负载均衡

    1.什么是LVS? 首 先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和 基于内容请求分发技术.调度器具 ...

  8. jsp分页完善版

    明天要考网络工程师了,而且这两天校运会,把那个分页的完善了下,明天考试,祈祷吧,我根本都没看书啊,所以只能去长见识了.100大洋啊,下个学期我想考考证了,不然以后出去麻烦了.呵呵,不多说还是说说自己对 ...

  9. org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported解决!

    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported解决 ...

  10. bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401

    这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...