洛谷 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和路径 题意 求无向带权图的最大异或路径 范围 思路还是很厉害的,上午想了好一会儿都不知道怎么做 先随便求出一颗生成树,然后每条返祖边都可以出现一个环,从的路径上 ...
随机推荐
- python循环,函数
平常的网页会转换成ascll码,在转编译成十六进制a='http://www.mywebsit.com/?query=python&count=20'print(a)b=a.replace(' ...
- python中的数据类型之元组和字典
一.元组:俗称不可变的列表,又被称为只读列表.元组用小括号括起来,里面可以放任何数据类型的数据,查询可以,循环也可以,切片也可以,但就是不能修改. 注意:如果元组中只有一个元素,一定要加一个逗号,否则 ...
- 嵌入式框架Zorb Framework搭建二:环形缓冲区的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- 阿里云mysql连接不上
轻量级服务器管理 - 防火墙 - 添加规则 防火墙 mysql 3306 注意IPtables 与 firewalld 状态! 啃爹的防火墙,找了一天
- lambda, 匿名函数, 变量,传参
lambda: # 无参数函数情况 def delete_one(): pass Button(otherFrame, text="删除", width=4, command=de ...
- Java RMI 入门指南
开通博客也有好些天了,一直没有时间静下心来写博文,今天我就把两年前整理的一篇关于JAVA RMI入门级文章贴出来,供有这方面需要的同学们参考学习. RMI 相关知识 RMI全称是Remote Meth ...
- 【数据库】 SQLite 介绍
[数据库] SQLite 介绍 一. 特点 : 小而精悍 1. 轻量级 : 占用资源低 2. 嵌入式 : 无需安装,直接引用就可用 3. 支持 SQL 语法, 大部分兼容 Sql Server 语法, ...
- Qt Charts_Audio实践
这里完全是照搬帮助文档中的代码生成的程序 上预览图 工程文件代码 #------------------------------------------------- # # Project crea ...
- Linux-Ps命令使用
ps -ef | grep java ps aux | grep java ps aux 是用BSD的格式来显示Java进程 显示的项目有: USER PID %CPU %MEM ...
- Google无法离线安装扩展程序
Google无法离线安装扩展程序 Chrome插件伴侣 按照里面的使用说明使用 网盘地址: 链接: https://pan.baidu.com/s/1eXoLXyPNl2pfoPnArHq2Lg 提取 ...