给定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. 关于bootstrap-table服务端分页问题

    昨天项目中涉及到了前端表格分页问题.数据一共有1万多条,所以选择了后端分页. 之前用的都是前端分页,第一次使用后端分页.网上也找到了一些例子,最后做出来了. 这里用的是bootstrap-table插 ...

  2. HDU 6214 最小割边

    双倍经验题:HDU 6214,3987 求最小割的最小边. 方案一: 首先跑最大流,这个时候割上都满载了,于是将满载的边 cap = 1,其他 inf ,再跑最大流,这个时候限定这个网络的关键边就是那 ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  4. maven常用依赖总结

    Apache工具组件 <!-- 字符串处理 --> <dependency> <groupId>org.apache.commons</groupId> ...

  5. 【Calculus 微积分の一些个人理解】

    微积分 微积分(Calculus)是高等数学中研究函数的微分(Differentiation).积分(Integration)以及有关概念和应用的数学分支.它是数学的一个基础学科.内容主要包括极限.微 ...

  6. http://blog.csdn.net/hhhccckkk/article/details/9313999

    http://blog.csdn.net/hhhccckkk/article/details/9313999

  7. bootstrapPaginator设置同步会翻2页的小坑

    因为需要用到post的返回值在做后面的决定.不想嵌套在回调函数中.网上找了一下.设置为同步 $.ajaxSetup( { async: false }); 结果bootstrap的翻页组件就出了bug ...

  8. GPU卡掉卡

    这几天用GPU卡跑东西,老是提示opencv的一个问题.但是我换个数据跑就没问题.说明代码是没问题的.发挥我作为女人的特质,从起试试吧.结果从起后找不到GPU卡了.nvidia-smi提示我没有安装最 ...

  9. 全文搜索引擎 Elasticsearch 安装踩坑记录

    一.安装 Elastic 需要 Java 8 环境.如果你的机器还没安装 Java 安装完 Java,就可以跟着官方文档安装 Elastic.直接下载压缩包比较简单. $ wget https://a ...

  10. 论REST架构与传统MVC

    一前言 :   由于 REST 可以降低开发的复杂度,提高系统的可伸缩性,增强系统的可扩展性,简化应用系统之间的集成,因而得到了广大开发人员的喜爱,同时得到了业界广泛的支持.比如 IBM,Google ...