题目链接: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. vue v-model使用说明

    1.概述 v-model 会忽略所有表单元素的 value.checked.selected 特性的初始值而总是将 Vue 实例的数据作为数据来源.你应该通过 JavaScript 在组件的 data ...

  2. 对datatable进行linq过滤

    实例: DataTable data = new DataTable(); data.Columns.Add("schoolid", Type.GetType("Syst ...

  3. ruby 作为嵌入脚本时使用的注意事项

    近期一直在採坑... 假设是作为嵌入式脚本使用ruby的话... 一定会遇到这2个问题... gem安装的,无法在嵌入时使用..为啥.? 由于你没require 'ruby gem' 出现 找不到 E ...

  4. BigDecimal的String类型

    java本身对浮点型的计算会丢失精度,这个一定要注意,必须要用BigDecimal的String类型才能解决精度的问题. BigDecimal一共有四个构造方法: 我们在计算商品价格的时候,一定要用B ...

  5. NativeBase准备工作

    环境 node>= 4.0 npm>= 3.0 rnpm (only if React Native version < 0.29) ReactNativeCLI  安装及运行 ht ...

  6. Visual Studio 2017 for Mac Preview

    Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...

  7. Mac修改默认python版本

    研究python爬虫,需要用到Beautiful Soup 但是Mac默认的python版本为2.7 自己安装了3.6的版本 import 报错 查找资料: Mac在启动,会先加载系统配置文件(包括~ ...

  8. Java正确获取客户端真实IP方法整理

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  9. andeoid硬件解码

    Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...

  10. c# combobox 绑定枚举方式

    建立一个类 : using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...