给定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. Java使用FileReader(file)、readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号。

    //Java使用FileReader(file).readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号. public static void readFi ...

  2. scrum3

    首先我一直做的是框架的设计,但不同的是这次我们整合完善了这个软件目前的所有需求也定义好了它的大题框架,总的来说设计部分已经结束,现在也就是本次冲刺,我们将重点进行整个软件的数据库编程环节,也就是用SQ ...

  3. 使用while循环+try-except定位元素

    selenium学习过程中,发现自己遇到的最大的困难不是那些元素的操作,而是元素的定位,有时候明明利用firebug将xpath路径确认好了,但是在定位元素的时候还是会报错,后来在度娘上找到了一个方法 ...

  4. 108. Convert Sorted Array to Binary Search Tree (building tree with resursion)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  5. bzoj2111 [ZJOI2010]排列计数

    Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...

  6. swift语言的特点(相对于oc)

    1.泛型.泛型约束与扩展: 2.函数式编程: 3.值类型.引用类型: 4.枚举.关联值.元组等其他 上述为swift最大的特点 Another safety feature is that by de ...

  7. PHP语言开发微信公众平台(订阅号)之注册(1)

    1.百度搜索"微信公众平台" 2.选择微信公众平台官网并单击打开 3.进入官网页面,单击 "立即注册" 进入注册页面 4.进入注册页面,单击订阅号 5.进入订阅 ...

  8. ettercap_缺少组件问题

    原因:缺少WinPcap组件解决:安装即可

  9. 【luogu P1144 最短路计数】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1144 #include <iostream> #include <cstdio> # ...

  10. Android学习笔记_76_Android ProgressBar 进度条

    android 进度条的样式  例1:(默认样式(中等圆形))Xml代码 <ProgressBar      android:id="@+id/progressBar1"   ...