题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4582

题意:

  给你n个数。

  让你将其中的一些数放入两个不同的集合中,并保证同一集合内两两元素相差不超过k。

  问你两个集合中数字个数之和最大为多少。

题解:

  贪心。

  先将所有数字升序排序。

  然后O(N)处理出ans[i]:

    以i为起点,在一个集合中,往后最多能选多少个数。

  再从后往前更新ans[i]和best:

    以i以及i后面的点为起点,在一个集合中,往后最多能选多少个数。

    ans[i] = max(ans[i], ans[i+1])

    同时更新答案:best = max ans[i] + ans[i+ans[i]]

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 50005 using namespace std; int n,k;
int best=;
int a[MAX_N];
int ans[MAX_N]; void read()
{
cin>>n>>k;
for(int i=;i<n;i++)
{
cin>>a[i];
}
} void solve()
{
sort(a,a+n);
int pos=;
for(int i=;i<n;i++)
{
while(a[pos]-a[i]<=k && pos<n) pos++;
ans[i]=pos-i;
}
ans[n]=;
for(int i=n-;i>=;i--)
{
best=max(best,ans[i]+ans[i+ans[i]]);
ans[i]=max(ans[i],ans[i+]);
}
} void print()
{
cout<<best<<endl;
} int main()
{
read();
solve();
print();
}

BZOJ 4582 [Usaco2016 Open]Diamond Collector:贪心【相差不超过k】的更多相关文章

  1. Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

    4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: ...

  2. BZOJ 4582: [Usaco2016 Open]Diamond Collector

    Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...

  3. bzoj4582[Usaco2016 Open]Diamond Collector

    bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装 ...

  4. [Usaco2016 Open]Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  5. [USACO16OPEN]钻石收藏家Diamond Collector

    由于相差不超过k才可以放在一起,要判断不超过k这个条件,显然我们需要排序 首先我们需要一个f数组,f[i]意义看代码开头注释, 假设我们可以选择的某一个区间是a[l]~a[r](已排序且最优(最长的意 ...

  6. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  7. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  8. BZOJ 4576: [Usaco2016 Open]262144

    Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...

  9. Diamond Collector

    Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...

随机推荐

  1. 【Excle数据透视表】如何隐藏数据透视表中行字段的”+/-”按钮

    如下图:新建的数据透视表中有存在"+/-"符号,导致数据透视图不太美观,那么怎么处理呢? 解决方案 单击"显示"组中的"+/-"按钮显示或隐 ...

  2. 转:css:Position

    http://www.cnblogs.com/polk6/archive/2013/07/26/3214847.html http://blog.sina.com.cn/s/blog_4bcf4a5e ...

  3. EasyUI这个框架用了好久了,总结一下遇到的问题和解决方法

    1. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法: 我们之所以在页面中,只要书写相应easyui的class,Easyui就能成功渲染页面,这是因为解析器在 ...

  4. linux下编译ffmpeg 引入外部库x264

    Found no assembler Minimum version is nasm-2.13 If you really want to compile without asm, configure ...

  5. 25:坐标移动CoordinateMove

    题目描述 开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动.从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面. 输入: 合 ...

  6. Java 调用OPENOFFIC 转换文档类型

    public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...

  7. 使用sphinx生成美观的文档

    先上效果图 详情 首先,须要知道什么是restructuredtext.能够理解为类似于markdown的一个东西. 然后 安装.pip install sphinx 进入存放文档的文件夹,在命令行, ...

  8. 安装部署zookeeper集群

    实验说明: 三台虚拟机做zookeeper集群,集群个数最好是奇数个,原理详见zookeeper 详解  安装zookeeper 请确保jdk 已安装好,否则无法启动  三台虚拟机IP分别为:192. ...

  9. linux 经常使用命令

    帮助信息 ./configure -help|grep mysql 安装php ./configure --prefix=/usr/local/fastphp --with-mysql=mysqlnd ...

  10. python 基础 5.3 类的重写

    一. 类的重写 只需要重新定义类的属性(变量),就是累的重写了 示例:重新定义类grandson的 name属性   #/usr/bin/python #coding=utf-8 #@Time :20 ...