给定N个数对$(T_i,S_i)$,表示时刻$S_i$时在位置$T_i$处出现一粒糖果。有一些机器人可供使用,每个机器人可花费一单位时间向相邻位置移动。要求用最少的机器人接到全部糖果。时刻0时机器人位置可自行安排。

$1 \leq N \leq100000$, $0 \leq S_i,T_i \leq 10^9$。

好题。首先可以想到转化成二维点对的问题。
对于两点$(x,y)$和$(p,q)$,两点之间能连线的要求是$|x-p| \geq |y-q|$。
要求用最少的折线覆盖所有的点。
我们只考虑时间小的向大的连边。不妨令$p<x$
不考虑相等的情况下,对$x,p$,$y,q$的大小关系进行讨论。
$\left\{\begin{array}{l}x>p\\y>q\end{array}\right.$,这时还需满足$x-p \geq y-q$;
$\left\{\begin{array}{l}x>p\\y<q\end{array}\right.$,这时还需满足$x-p \geq q-y$;
而如果$p>x$时
$\left\{\begin{array}{l}p>x\\y>q\end{array}\right.$或$\left\{\begin{array}{l}p>x\\q>y\end{array}\right.$
发现符合条件的两种情况都满足$x-y \geq p-q$且$x+y \geq p+q$。而两种不合法的均不满足两者其一。
于是就转化成了二维偏序,,,点i的两个关键字如果都比点j大则可以连边。等于也是可以的。
贪心的思路比较显然,保证$x+y$有序的前提下,找到$x-y$与小于等于当前$x-y$值,且差最小的那个。(更小的一定不更优)。如果找不到就新添加一个机器人。用set维护即可,复杂度$O(nlogn)$。
#include<bits/stdc++.h>
using namespace std;
const int N=;
typedef pair<int,int> P;
inline int read(){
int r=,c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))
r=r*+c-'',c=getchar();
return r;
}
int ans,n;P a[N];
set<int>S;
set<int>::iterator it;
int main(){
ans=n=read();
for(int i=;i<=n;i++){
int y=read(),x=read();
a[i]=P(x+y,x-y);
}
sort(a+,a+n+);
for(int i=;i<=n;i++){
int t=a[i].second;
it=S.upper_bound(t);
if(it!=S.begin())
S.erase(--it),ans--;
S.insert(t);
}
printf("%d\n",ans);
}
 

[bzoj1359][Baltic2009]Candy的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  4. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  7. 【leetcode】Candy

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  9. [LintCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

随机推荐

  1. 最简单的dockerfile使用教程 - 创建一个支持SSL的Nginx镜像

    什么是dockerfile?简单的说就是一个文本格式的脚本文件,其内包含了一条条的指令(Instruction),每一条指令负责描述镜像的当前层(Layer)如何构建. 下面通过一个具体的例子来学习d ...

  2. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  3. Error:Cannot determine Java VM executable in selected JDK

    http://devnet.jetbrains.com/message/5521484#5521484 Configure -> Project defaults -> Project s ...

  4. Java8函数之旅 (七) - 函数式备忘录模式优化递归

    前言 在上一篇开始Java8之旅(六) -- 使用lambda实现Java的尾递归中,我们利用了函数的懒加载机制实现了栈帧的复用,成功的实现了Java版本的尾递归,然而尾递归的使用有一个重要的条件就是 ...

  5. AngularJS显示一个简单表格

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. element 表单的input循环生成,并可单个input失去焦点单个验证并保存; (多个表单实例)

    <div class="box_item"> <el-form ref="aList" :model="form" :ru ...

  7. Question20171231 聊聊为什么32bit只能支持4GB内存

    1. 32位系统最大只能支持4GB内存之由来 也许大家对这个问题都不陌生,实际装过系统用过电脑的朋友可能都有这样的经历:自己电脑配的是4G的内存条,可是装完系统之后发现电脑上显示的只有3.2G左右可用 ...

  8. datagrid中设置编辑,删除列是否可以访问

    foreach (RepeaterItem Item in rpt_Result.Items) { LinkButton edit = (LinkButton)Item.FindControl(&qu ...

  9. Evercookie

    1. Evercookie Evercookie是一个Javascript API,可以在浏览器中生成极其持久的cookie. 它的目标是在客户删除标准cookie,Flash cookie(本地共享 ...

  10. 【TOJ 3692】紧急援救

    #include<iostream> #include<algorithm> #include<queue> using namespace std; #defin ...