bzoj4582[Usaco2016 Open]Diamond Collector
bzoj4582[Usaco2016 Open]Diamond Collector
题意:
n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装多少个。n最大50000。
题解:
我真傻,真的~首先对大小排序,然后找以i为左端点的可装区间,这个操作两个指针就可以搞,我却以为要二分查找。预处理完了,因为不交错的区间肯定比交错的区间优,所以从n到1递推一下从n到i最大的区间大小是多少,然后枚举每个区间,找到当前区间大小加上右端点+1到n中最大的区间大小中的最大值输出即可。我却以为要找与最大区间不交错的第二大区间,结果WA了好几发,才发现这是错误的贪心QAQ
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define maxn 60000
using namespace std; int n,k,sz[maxn],r,cnt[maxn],mx[maxn];
int main(){
scanf("%d%d",&n,&k); inc(i,,n)scanf("%d",&sz[i]); sort(sz+,sz+n+); r=;
inc(i,,n){
while(r<=n&&sz[r]-sz[i]<=k)r++; cnt[i]=r-i;
}
mx[n+]=; dec(i,n,)mx[i]=max(mx[i+],cnt[i]);
int ans=; inc(i,,n)ans=max(ans,cnt[i]+mx[i+cnt[i]]); printf("%d",ans);
return ;
}
20160519
bzoj4582[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: ...
- BZOJ 4582: [Usaco2016 Open]Diamond Collector
Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...
- BZOJ 4582 [Usaco2016 Open]Diamond Collector:贪心【相差不超过k】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4582 题意: 给你n个数. 让你将其中的一些数放入两个不同的集合中,并保证同一集合内两两元 ...
- [Usaco2016 Open]Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- Diamond Collector
Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- Diamond Collector (动态规划)
问题 I: Diamond Collector 时间限制: 1 Sec 内存限制: 64 MB提交: 22 解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, alwa ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 【BZOJ 4582】【Usaco2016 Open】Diamond Collector
http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...
随机推荐
- rust 神奇的特质
pub trait Summary { fn summarize_author(&self) -> String; fn summarize(&self) -> Strin ...
- cb33a_c++_STL_算法_查找算法_(6)binary_search_includes
cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...
- 布局之: flex(CSS3新增)
flex 基本概念 flex布局(flex是flexible box的缩写), 也称为弹性盒模型 .将属性和属性值(display:flex; )写在哪个标签样式中,谁就是 容器:它的所有子元素自动成 ...
- Linux MySQL Proxy 读写分离
导读 因为读写分离是建立在MySQL集群主从复制的基础上,还不了解的,先看我另一篇博客:点我直达 MySQL-Proxy简介 mysql-proxy是mysql官方提供的mysql中间件服务,上游可接 ...
- jmeter的beanshell
[beanshell] 简单介绍beanshell,小型的java源代码解释器 运行下beanshell [常用命令] print() 输出内容到命令行中 (1)也可以在beanshell中自定义 [ ...
- Spring Boot 整合 Apollo
简介 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场景 ...
- Spring声明周期的学习心得
我们首先来看下面的一个案例: 这里是 HelloWorld.java 文件的内容: package com.yiibai; public class HelloWorld { private ...
- 洛谷P1220关路灯【区间dp】
题目描述 某一村庄在一条路线上安装了 \(n\) 盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯 ...
- CI CD概念
CI:持续集成 Continuous Integration CD:持续发布 Continuous Delivery (完)
- CentOS 关闭暂不需要的系统服务
需要保留的服务:crond.iptables.irqbalance.microcode_ctl.network.random.sshd.syslog.local 一 .使用命令:ntsysv 打开选项 ...