问题 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. uva10884 Persephone

    题目戳这里. 找规律. 每一列占据的格子一定是一段区间: 相邻列之间的区间有交. 上界先增后减,下界先减后增. \(f_{i,j,k,0/1,0/1}\)表示考虑前\(i\)列,第\(i\)列,上界为 ...

  2. 两个数组的交集 II [ LeetCode - 350 ]

      原题地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/description/   给定两个数组,写一个方法来计算 ...

  3. Linux内存 性能调优

    内存是影响Linux性能的主要因素之一,内存资源的充足与否直接影响应用系统的使用性能. free命令:监控Linux内存使用状况. 由上图可知,空闲内存是free+buffers+cached=155 ...

  4. ERROR: Found lingering reference file hdfs

    Found lingering reference异常 ERROR: Found lingering reference file hdfs://jiujiang1:9000/hbase/month_ ...

  5. O(n^2)以及O(nlogn)时间复杂度的排序算法

    O(n^2)的算法 都是做的升序. 简单选择排序 思路:每次选择还未排序的区间的最小值和未排序区间的第一个值交换. function selectSort(arr){ for(let i = 0; i ...

  6. jquery学习总计

    1,jquery的基础语法 $(selector).action(); 选择器(selector)查询和查找html元素,action()执行对函数的操作. 2.选择器 id,类,类型,属性,属性值等 ...

  7. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...

  8. Idea导入的工程看不到src等代码

    问题描述: 从其他地方拷贝过来的工程,在本地导入到idea中时,展示如下的页面,里面的其他文件都看不到. 解决办法:(不知道是具体的什么原因引起的) 1. 关闭IDEA, 2.然后删除项目文件夹下的. ...

  9. bzoj3669: [Noi2014]魔法森林 lct版

    先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...

  10. kernel 中 sscanf和sprintf()函数使用说明【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/8480695 在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于s ...