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 解题报告的更多相关文章

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

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

  2. 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

        解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...

  3. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector

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

  4. 洛谷P3143 [USACO16OPEN]钻石收藏家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. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  7. 【洛谷】CYJian的水题大赛 解题报告

    点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...

  8. 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 解题报告

    P1337 [JSOI2004]平衡点 / 吊打XXX 题目描述 有 \(n\) 个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.\(X\)处就是公共的绳结.假设 ...

  9. 洛谷 P4151 [WC2011]最大XOR和路径 解题报告

    P4151 [WC2011]最大XOR和路径 题意 求无向带权图的最大异或路径 范围 思路还是很厉害的,上午想了好一会儿都不知道怎么做 先随便求出一颗生成树,然后每条返祖边都可以出现一个环,从的路径上 ...

随机推荐

  1. [转]ThinkPHP5 隐藏index.php问题

    ThinkPHP5 隐藏index.php问题 Apache,修改.htaccess文件 ----------------------------------------------------- R ...

  2. CentOS 同步时间的方法

    与时间服务器上的时间同步的方法 1.  安装ntpdate工具 # yum -y install ntp ntpdate 2.  设置系统时间与网络时间同步 # ntpdate cn.pool.ntp ...

  3. 8.1 编写USB鼠标驱动程序,并测试

    学习目标:编写USB鼠标驱动程序,并测试(将USB鼠标的左键当作L按键,将USB鼠标的右键当作S按键,中键当作回车按键). 一.怎么写USB设备驱动程序?步骤如下: 1. 首先先定义全局变量usb_d ...

  4. Leecode刷题之旅-C语言/python-58.最后一个单词的长度

    /* * @lc app=leetcode.cn id=58 lang=c * * [58] 最后一个单词的长度 * * https://leetcode-cn.com/problems/length ...

  5. 【JDBC】一、JDBC连接数据库

    package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLExce ...

  6. Qt之pro文件解析

    在我们创建Qt工程项目时,Qt Creator总会创建一个.pro文件,我们称.pro文件为Qt的工程管理文件.一个工程项目可以包含一个或多个.pro文件.理解和掌握pro文件的用法,将有利于Qt开发 ...

  7. 让CPU使用率正弦变化

    网络上流传一个面试题,说如何编程让CPU的使用率按照正弦方式变化 代码如下(运行环境Linux): #include <stdio.h> #include <stdlib.h> ...

  8. go学习笔记-程序测试

    程序测试 测试是一个可重复的过程,它验证某个东西是否按预期工作.一般通过 go test 进行测试,步骤如下 首先,是我们的文件名.Go 要求所有的测试都在以 _test.go 结尾的文件中.这使得我 ...

  9. CSS3实现加载数据动画2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. pprof 查看goroutine

    package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...