洛谷P3088 挤奶牛
这个题也是一个单调队列来优化的 $ dp $ ,我们考虑这个题,这个题让我们求出有多少奶牛会觉得拥挤,如果我们还像琪露诺那个题那样单纯用一次单调队列肯定是不行的,因为牛觉不觉得拥挤是受左右的影响,所以我们选择从前往后、从后往前用两遍单调队列。
一开始就是在 $ push $ 元素的时候,如果要 $ push $ 的元素是队尾元素高度的两倍,那么就给队尾的元素打一个标记,然后 $ tail-- $
但是这样的做法会造成漏判,所以,我们要换一种判断的方法。就是把判断从当前元素判断队中的元素变为队中的元素判断当前元素。(先后问题)
在循环中,合法性判断完之后,如果队头的元素是当前元素高度的两倍,那么把当前元素打一个标记,这样就完美解决了漏判的问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 50005;
inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1; ch = getchar();}
while(ch >= '0' &&ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
}
int n,d;
int q[maxn],head,tail;
bool l[maxn],r[maxn];
struct Edge{
int x,high;
}e[maxn];
bool cmp(Edge a,Edge b){
return a.x < b.x;
}
int main(){
n = read(); d = read();
for(int i=1;i<=n;i++){
e[i].x = read();
e[i].high = read();
}
sort(e + 1 , e + 1 + n , cmp);
head = 1; tail = 0;
for(int i=1;i<=n;i++){
while(head <= tail && e[i].high >= e[q[tail]].high)
tail--;
q[++tail] = i;
while(head <= tail && e[i].x - e[q[head]].x > d)
head++;
if(e[q[head]].high >= 2 * e[i].high)
l[i] = 1;
}
head = 1; tail = 0;
for(int i=n;i>=1;i--){
while(head <= tail && e[i].high >= e[q[tail]].high)
tail--;
q[++tail] = i;
while(head <= tail && e[q[head]].x - e[i].x > d)
head++;
if(e[q[head]].high >= 2 * e[i].high)
r[i] = 1;
}
int ans = 0;
for(int i=1;i<=n;i++)
if(l[i] && r[i]) ans++;
printf("%d",ans);
return 0;
}
洛谷P3088 挤奶牛的更多相关文章
- 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解
P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...
- 【洛谷P2340】 奶牛会展
\(奶牛会展\) 题目链接 由于智商之和或情商之和不能为负数,所以直接把智商+情商>0的奶牛加上是布星的 我们考虑背包,不妨将智商当做物品大小,将情商当做价值 我们要求 大小+价值 的最大值 \ ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows
题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving va ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷 P1824 进击的奶牛
P1824 进击的奶牛 题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi&l ...
- 洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows 题解
P2858 [USACO06FEB]奶牛零食Treats for the Cows 题目描述 FJ has purchased N (1 <= N <= 2000) yummy treat ...
- 洛谷 [P1578] WC2002 奶牛浴场
本题是一道用极大化思想求最大子矩阵的经典题目.这个题目很出名,可以在百度搜索王知昆国家队dalao的论文,其中说的非常详细. 先枚举极大子矩形的左边界,然后从左到右依次扫描每一个障碍点,并不断修改可行 ...
随机推荐
- 【noip模拟】D(==)
Portal --> who knows == Description 数轴上面有一些洞,有一些老鼠,每个洞有一个容量限制,一只位于\(x\)的老鼠进到位于\(y\)的洞要花费\(|x-y|\) ...
- 洛谷P4382 劈配
不知道这个Zayid是谁... 题意: 有n个人,m个导师.每个导师能接纳bi个人,每个人对于这m个导师都有一个志愿档次. 优先满足靠前的人,问到最后每个人匹配的导师是他的第几志愿. 每个人又有一个限 ...
- python对MySQL的CRUD
我是闲的没事干,2014过的太浮夸了,博客也没写几篇,哎~~~ 用这篇来记录即将逝去的2014 python对各种数据库的各种操作满大街都是,不过,我还是喜欢我这种风格的,涉及到其它操作,不过重点还是 ...
- python安装pip、numpy、scipy、statsmodels、pandas、matplotlib等
1.安装python 2.安装numpy(开源的数值计算扩展,可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多. 很多库都是以此库为依 ...
- 【Asp.net入门4-02】使用Visual Studio调试器
- NOIP2011 提高组 Day1
自测:8:27——11:51 实际得分:100+60+20=180 期望得分:100+60+40=200 T3读错题,失20 http://cogs.pro/cogs/page/page.php?ai ...
- seaborn基础整理
seaborn是基于matplotlib的更高级的做图工具,下面主要针对以下几个部分进行整理: 第一部分:https://douzujun.github.io/page/%E6%95%B0%E6%8D ...
- Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham
Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham Ben Graham finished at the top of the ...
- 深入分析tcp close与shutdown
关闭socket-close 我们知道,tcp是一种支持全双工(full-duplex)通信的的协议,也就是说建立连接的两端可以在同一个时刻发送.接受数据.在需要关闭套接字的时候,我们一般调用: in ...
- 重新找回spyder3-editor 里的code completion
升级到spyder3之后, 突然丢失了code autocompletion在editor context里. 觉得太不爽了. 虽然在ipython窗格里TAB键的自动完成功能依然完好. 仔细观察 T ...