Diamond Collector (动态规划)

问题 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=50010;
int n,dp[N],len,g[N];
ll k,a[N];
int w[21][21];
int flag=0;
int vis[10000]={0};
string str[21],ch;
int maxn=1;
map<string,int>p,pp;

int main() {
    memset(dp,0,sizeof(dp));
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    int m=1,r=1;
    for(int i=1;i<=n;i++)
    {
       while(a[r+1]-a[i]<=k&&r<n) r++;
       dp[i]=r-i+1;
    }
    for(int i=n;i>=1;i--)
        g[i]=max(g[i+1],dp[i]);
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,dp[i]+g[i+dp[i]]);
    cout<<ans<<endl;
    return 0;
}

时间: 2024-10-18 08:37:47

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

4582: [Usaco2016 Open]Diamond Collector

4582: [Usaco2016 Open]Diamond Collector Description 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 t

Diamond Collector

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 case

bzoj4582[Usaco2016 Open]Diamond Collector

bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装多少个.n最大50000. 题解: 我真傻,真的~首先对大小排序,然后找以i为左端点的可装区间,这个操作两个指针就可以搞,我却以为要二分查找.预处理完了,因为不交错的区间肯定比交错的区间优,所以从n到1递推一下从n到i最大的区间大小是多少,然后枚举每个区间,找到当前区间大小加上右端点+1到n中最大的

Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 136[Submit][Status][Discuss] Description Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! Sh

P3143 [USACO16OPEN]钻石收藏家Diamond Collector[two-pointers]

P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题意要注意一点:有两个陈列架! 如果只有一个陈列架,是很容易的.two-pointers直接从左到右跑一下即可. 如果有两个陈列架,就需要进行答案合并了.做法是这样的: 设两个数组: \(pre\)数组,\(pre[i]\)表示以\([1,i]\)为右端点时区间长度的最大值. \(suf\)数组,\(suf[i]\)表示以\([i,n]\)为左端点时区间长度的最大值. 两个数组分别是前缀最大值和后缀最大值.同样利

Bone collector//动态规划

题目 Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 89519 Accepted Submission(s): 36853 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone

hdu2602Bone Collector ——动态规划(0/1背包问题)

Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …The bone collector had a big bag with a volume of

BZOJ 4582: [Usaco2016 Open]Diamond Collector

Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这个玩意可以二分也可以用单调队列,随便搞啊... 然后统计答案就是枚举第二个序列的起点,然后往后扫的时候统计一下,第一个序列的最大长度就可以了. Code /************************************************************** Problem:

洛谷P3143 [USACO16OPEN]钻石收藏家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  diamonds () of varying sizes, and she wants to arrange some of them in a pair of display cases in the barn. Since Bessie