洛谷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的论文,其中说的非常详细. 先枚举极大子矩形的左边界,然后从左到右依次扫描每一个障碍点,并不断修改可行 ...
随机推荐
- Linux编程中 #define _XOPEN_SOURCE的作用
[误解]#define _XOPEN_SOURCE决不是简单的宏定义它是使程序符合系统环境的不可缺少的部分 [概念]Glibc 所实现全部或部分规范下的功能有:1.ISO C: C语言国际标准. 2. ...
- Android Layout: TableLayout
TableLayout<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" andr ...
- 【数学】【CF1091D】 New Year and the Permutation Concatenation
Description 给定一个数 \(n\),将所有 \(1~\sim~n\) 的排列按照字典序放到一个序列中,求有多少长度为 \(n\) 的子序列 \(p_i~p_{i+1}~\dots~p_{i ...
- mac、linux 查看端口占用程序
lsof -i:80 列出占用 80 端口的程序 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 672 ruby 42u IPv4 ...
- ICMP类型和代号对照表
ICMP类型 TYPE CODE Description Query Error 0 0 Echo Reply——回显应答(Ping应答) x 3 0 Network Unreachable——网 ...
- Lowest Common Ancestor in a Binary Tree
二叉树最低公共祖先节点 acmblog If one key is present and other is absent, then it returns the present key as LC ...
- GO_11:GO语言基础之并发concurrency
并发Concurrency 很多人都是冲着 Go 大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码的解析来看,goroutine 只是由官方实现的超级“线程池”而已.不过话说回来,每个实例 4-5KB的 ...
- P4888 三去矩阵
P4888 三去矩阵 给出一个字符矩阵, 多次询问求以 \((x, y)\) 为中心的最长回文串长度(即横竖两种) \(l, q <= 2000\) Solution 数据范围小直接模拟即可 C ...
- openstack指南
1.openstack官网 http://www.openstack.org/ 2.openstack源码地址 https://github.com/openstack 3.openstack的pac ...
- vue中的slot与slot-scope
深入理解vue中的slot与slot-scope vue+element-ui+slot-scope或原生实现可编辑表格 vue插槽详解