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 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.

Input

The first line of the input file contains N and K (0≤K≤1,000,000,000). The next NN lines each cont

ain an integer giving the size of one of the diamonds. All sizes will be positive and will not excee

d 1,000,000,000

Output

Output a single positive integer, telling the maximum number of diamonds that Bessie can showcase in

total in both the cases.

Sample Input

7 3
10
5
1
12
9
5
14

Sample Output

5

题解:

先讲一下题意,大致说是将n个数选出几个数放入两个箱子中,一个箱中的最大数减最小数不得超过k,使得选出的数个数最大化。

首先这题有一个比较明显的特性:

两个箱子中放的数肯定是排完序后连续的一段,我们可以先将从第i个点出发的最大长度算出,在枚举分割点就行了(我是枚举起点的)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int n,m,i,r,a[50005],f[50005],g[50005],ans;
int main()
{
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    r=1;
    for(i=1;i<=n;i++)
    {
        while(a[r+1]-a[i]<=m&&r<n) r++;
        f[i]=r-i+1;
    }
    for(i=n;i>=1;i--)
        g[i]=max(g[i+1],f[i]);
    ans=0;
    for(i=1;i<=n;i++)
        ans=max(ans,f[i]+g[i+f[i]]);
    printf("%d",ans);
    return 0;
}
时间: 2024-10-10 04:16:41

4582: [Usaco2016 Open]Diamond Collector的相关文章

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

BZOJ 4582: [Usaco2016 Open]Diamond Collector

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

bzoj4582[Usaco2016 Open]Diamond Collector

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

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 sh

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

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

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

洛谷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

洛谷 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 NN diamonds (N \leq 50,000N≤50,000) of varying sizes, and she wants to arrange some of them in a pair of display cases in

Luogu P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

又是一个学数据结构学傻了的人 才不会承认是看到Splay,觉得可以写平衡树才进来的呢 Description: 给出一个序列,问排序后,选取两个区间,使其没有重合部分,且每个区间右端点减去左端点不大于k,求这两个区间长度之和的最大值. 前置技能:FHQ-Treap.线段树 (不会的出门百度) Solution: 看数据范围,5e4,很好,O(nlogn)完全可以水过去.那么我们可以放心的考虑FHQ了.因为要最优,所以我们要找到每一个点,在有序序列中对应的满足题目条件的区间的大小,这就可以用FHQ