洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
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 \(N\) diamonds (\(N \leq 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.
奶牛Bessie很喜欢闪亮亮的东西(BalingBaling),所以她喜欢在她的空余时间开采钻石!她现在已经收集了N颗不同大小的钻石(N<=50,000),现在她想在谷仓的两个陈列架上摆放一些钻石。
Bessie想让这些陈列架上的钻石保持相似的大小,所以她不会把两个大小相差K以上的钻石同时放在一个陈列架上(如果两颗钻石的大小差值为K,那么它们可以同时放在一个陈列架上)。现在给出K,请你帮Bessie确定她最多一共可以放多少颗钻石在这两个陈列架上。
输入输出格式
输入格式:
The first line of the input file contains \(N\) and \(K\) (\(0 \leq K \leq 1,000,000,000\)).
The next NN 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.
方法很多而且都很神奇
我的做法是:对点排序,枚举每个点作为左端点,二分一个区间的左端点超过它的右端点的位置。
在这个位置的右边选择一个最大的,在这个位置左边选择最大的,都可以事先维护(把区间重复和不重复分开统计)
注意预处理时也需要一些二分
Code:
#include <cstdio>
#include <algorithm>
const int N=5e4+10;
const int inf=0x3f3f3f3f;
int max(int x,int y){return x>y?x:y;}
int n,k,a[N],cntl[N],cntr[N],cntf[N],fr[N],fr2[N];
std::pair <int,int > b[N];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i].second=a[i];
b[i].first=b[i].second-k;
}
std::sort(a+1,a+1+n);//原来的
std::sort(b+1,b+1+n);//充当右端点后以左端点为关键字
for(int i=1;i<=n;i++)//这个自己充当右端点可以覆盖左边的几个(不考虑左端点)?
cntf[i]=fr[i]=i;
for(int i=1;i<=n;i++)//自己充当左端点右边覆盖几个?
{
if(a[n]<=b[i].second+k) cntl[i]=n-cntf[i]+1;
else cntl[i]=std::upper_bound(a+1,a+1+n,b[i].second+k)-a-1-cntf[i]+1;
}
for(int i=n;i;i--)//这个自己充当右端点可以覆盖左边的几个(考虑左端点)?
{
if(a[1]>=b[i].second-k) cntr[i]=cntf[i];
else cntr[i]=cntf[i]-(std::lower_bound(a+1,a+1+n,b[i].second-k)-a-1);
fr2[i]=max(fr2[i+1],cntr[i]);
}
int ans=0;
for(int i=1;i<=n;i++)//枚举自己当左端点的点
{
if(b[i].second+k>b[n].first)
{
ans=max(ans,fr[n]-cntf[i]+1);
}
else
{
std::pair <int,int> d=std::make_pair(b[i].second+k,inf);
int pos=std::upper_bound(b+1,b+1+n,d)-b;
ans=max(ans,cntl[i]+fr2[pos]);
ans=max(ans,fr[pos]-cntf[i]+1);
}
}
printf("%d\n",ans);
return 0;
}
2018.8.30
洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告的更多相关文章
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- 洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- [USACO16OPEN]钻石收藏家Diamond Collector
由于相差不超过k才可以放在一起,要判断不超过k这个条件,显然我们需要排序 首先我们需要一个f数组,f[i]意义看代码开头注释, 假设我们可以选择的某一个区间是a[l]~a[r](已排序且最优(最长的意 ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...
- 【洛谷】CYJian的水题大赛 解题报告
点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...
- 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 解题报告
P1337 [JSOI2004]平衡点 / 吊打XXX 题目描述 有 \(n\) 个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.\(X\)处就是公共的绳结.假设 ...
- 洛谷 P4151 [WC2011]最大XOR和路径 解题报告
P4151 [WC2011]最大XOR和路径 题意 求无向带权图的最大异或路径 范围 思路还是很厉害的,上午想了好一会儿都不知道怎么做 先随便求出一颗生成树,然后每条返祖边都可以出现一个环,从的路径上 ...
随机推荐
- MySQL innodb表使用表空间物理文件复制或迁移表
MySQL InnoDB引擎的表通过拷贝物理文件来进行单表或指定表的复制,可以想到多种方式,今天测试其中2种: 将innodb引擎的表修改为Myisam引擎,然后拷贝物理文件 直接拷贝innodb的表 ...
- Hadoop(8)-HDFS的读写数据流程以及机架感知
1. HDFS的写数据流程 1.客户端通过fs模块向NameNode申请文件上传,NameNode检查请求是否合法,如用户权限,目标文件是否已存在,父目录是否存在等等 2.NameNode返回是否可以 ...
- ant + jmeter 自动化接口测试环境部署
1.jdk下载安装 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.jmeter下载 jmeter官 ...
- CSS3实现加载数据动画1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用sqoop将mysql中表导入hive中报错
[hdfs@node1 root]$ sqoop import --connect jdbc:mysql://node2:3306/cm?charset-utf8 --username root -- ...
- c#根据ip获取城市地址
用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...
- 【APUE】Chapter5 Standard I/O Library
5.1 Introduction 这章介绍的standard I/O都是ISOC标准的.用这些standard I/O可以不用考虑一些buffer allocation.I/O optimal-siz ...
- 树莓派i2c功能
默认i2c是关闭的,用raspi-config 命令,会弹出一个配置框图 选择enable i2c就可以了 reboot之后 没有在/dev/目录下发现i2c-x的设备,这个时候需要做以下操作 1.添 ...
- js中DOM 节点的一些操作方法
什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定的一个规范. DOM就是由节点组成的. 解析过程 ...
- json格式化显示样式js代码分享
最近开发中需要在页面展示json.特整理了下代码,送给大家,希望能帮到有同样需求的朋友们. 代码: <html> <script src="http://cdn.bootc ...