Diamond Collector (动态规划)
问题 I: Diamond Collector
时间限制: 1 Sec 内存限制: 64 MB
提交: 22 解决: 7
[提交][状态][讨论版]
题目描述
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.
输入
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.
输出
样例输入
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 (动态规划)的更多相关文章
- Diamond Collector
Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解
4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 204 Solved: ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- bzoj4582[Usaco2016 Open]Diamond Collector
bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装 ...
- hdu2602Bone Collector ——动态规划(0/1背包问题)
Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...
- 【BZOJ 4582】【Usaco2016 Open】Diamond Collector
http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...
- BZOJ 4582: [Usaco2016 Open]Diamond Collector
Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...
- 【洛谷P3143】Diamond Collector
算是一道dp 首先,排序好每一个架子上都是一段区间,然后只需要统计每个点向左向右最长延伸的区间. 所以我们预处理出每个点以左.以右最大能延伸的长度(最多能选几个差值不超过k的) 然后枚举每个点作为断点 ...
随机推荐
- [洛谷P3690]【模板】Link Cut Tree (动态树)
题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作.操作有$4$种.操作从$0到3编号.点从1到n编号. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和. ...
- [洛谷P3743]kotori的设备
题目大意:ことり有$n$个设备,每个设备每秒共减少$a_i$能量(也就是说每一瞬间都在减少,而不是在一个时刻突然减少),开始前有$b_i$能量,ことり还有一个充电宝,无限能量,每秒共可以提供$p$的能 ...
- BZOJ5322 [Jxoi2018]排序问题 【贪心】
题目链接 BZOJ5322 题解 意思就是使有序的排列尽量少 就是使相同的数尽量少 然后大力贪心即可 #include<algorithm> #include<iostream> ...
- CSS3中transform属性的用法
有时候网站也要愚弄一下访客,比如愚人节.下面我给大家推荐个效果,就是整个页面左右颠倒了.css3 很强大,简单的几行代码就可以帮我们实现这个效果. view source print? 01 &l ...
- LVS+Keepalived搭建MyCAT高可用負載均衡集群
1.前面我们已经搭建好mysql主主,并且用mycat实现双写功能,主要配置文件: [root@mycat2 conf]# cat schema.xml <?xml version=" ...
- Spring学习--实现 FactoryBean 接口在 Spring IOC 容器中配置 Bean
Spring 中有两种类型的 bean , 一种是普通的 bean , 另一种是工厂 bean , 即 FactroyBean. 工厂 bean 跟普通 bean 不同 , 其返回的对象不是指定类的一 ...
- CSS3学习笔记之立体线框球形动画
效果截图: HTML代码: <div class="ball-box"> <div class="ball"> <div clas ...
- 复选框 checkbox 选中事件
项目中用的jquery-1.11 今天需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined ...
- jsp分页完善版
明天要考网络工程师了,而且这两天校运会,把那个分页的完善了下,明天考试,祈祷吧,我根本都没看书啊,所以只能去长见识了.100大洋啊,下个学期我想考考证了,不然以后出去麻烦了.呵呵,不多说还是说说自己对 ...
- 【LA4670-Dominating Patterns】AC自动机
http://acm.hust.edu.cn/vjudge/problem/19224 题意:给定n个单词,一个字符串,问哪些单词在字符串中出现的次数最多.单词aba,文本ababa,则aba出现了2 ...