问题 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. POJ2406 Power Strings 【KMP 或 后缀数组】

    电源串 时间限制: 3000MS   内存限制: 65536K 提交总数: 53037   接受: 22108 描述 给定两个字符串a和b,我们定义a * b是它们的连接.例如,如果a =" ...

  2. BZOJ3529 [Sdoi2014]数表 【莫比乌斯反演】

    3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2151 Solved: 1080 [Submit][Status ...

  3. IDEA2017 使用(二)

    1.鼠标悬浮在方法上显示api 2.关闭拼写检查 3.自动导入包(存在多个包时需要手动导入) 4.设置方法线

  4. 如何解决DuplicateFileException: Duplicate files copied in APK问题

    问题:有重复的文件存在APK里 解决方案:在Module里的build.gradle中设置忽略此重复文件即可.

  5. 【洛谷 P3469】[POI2008]BLO-Blockade(割点)

    题目链接 题意:一个无向联通图,求删去每个点及其所有边后有多少有序点对的连通性发生了变化. Tarjan求割点的例题.. 如果当前点不是割点,那么它对整个图的连通性不产生影响,只有自己与其他\(n-1 ...

  6. ggplot2绘制多图

    参考链接:http://www.cnblogs.com/nxld/p/6065237.html ggplot2.multiplot是一个易于使用的功能,将多个图形在同一页面上使用R统计软件和GGPLO ...

  7. 【反演复习计划】【COGS2433】&&【bzoj3930,CQOI2015选数】爱蜜莉雅的冰魔法

    同bzoj3930. (日常盗题图) #include<bits/stdc++.h> #define N 1000010 #define yql 1000000007 #define ll ...

  8. 【bzoj4810】由乃的玉米田

    lxl丧心病狂-- 首先允许离线的区间询问一看就是莫队.那么我们看下怎么莫队? 不会. "由乃题多半是不可做的."于是我看了下题解--好吧果然是bitset 用bitset维护当前 ...

  9. mybatis插入值的时候返回对象的主键值

    mapping文件: <insert id="insert" parameterType="com.vimtech.bms.business.riskproject ...

  10. golang命令行参数解析

    package main import ( "fmt" "os" ) func main(){ s:= os.Args fmt.Println(s) } 直接执 ...