从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了。

----------------------------------------------------------------------------

#include<bits/stdc++.h>
 
#define rep(i, n) for(int i = 0; i < n; ++i)
#define clr(x, c) memset(x, c, sizeof(x))
#define foreach(i, x) for(__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
 
using namespace std;
 
const int maxn = 50009;
 
struct R {
int p, h;
inline void Read() {
scanf("%d%d", &p, &h);
}
bool operator < (const R &t) const {
return p < t.p;
}
} A[maxn];
 
bool l[maxn], r[maxn];
int n, d;
deque<int> Q;
 
int main() {
freopen("test.in", "r", stdin);
cin >> n >> d;
rep(i, n) A[i].Read();
sort(A, A + n);
clr(l, 0), clr(r, 0);
while(!Q.empty()) Q.pop_back();
rep(i, n) {
R* h = A + i;
while(!Q.empty() && A[Q.front()].p + d < h->p) Q.pop_front();
if(!Q.empty() && A[Q.front()].h >= 2 * h->h) l[i] = true;
while(!Q.empty() && A[Q.back()].h < h->h) Q.pop_back();
Q.push_back(i);
}
while(!Q.empty()) Q.pop_back();
for(int i = n - 1; ~i; i--) {
R* h = A + i;
while(!Q.empty() && A[Q.front()].p - d > h->p) Q.pop_front();
if(!Q.empty() && A[Q.front()].h >= 2 * h->h) r[i] = true;
while(!Q.empty() && A[Q.back()].h < h->h) Q.pop_back();
Q.push_back(i);
}
int ans = 0;
rep(i, n) if(l[i] && r[i]) ans++;
cout << ans << "\n";
return 0;
}

----------------------------------------------------------------------------

3314: [Usaco2013 Nov]Crowded Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 120  Solved: 85
[Submit][Status][Discuss]

Description

Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has height h(i) (1 <= x(i),h(i) <= 1,000,000,000). A cow feels "crowded" if there is another cow at least twice her height within distance D on her left, and also another cow at least twice her height within distance D on her right (1 <= D <= 1,000,000,000). Since crowded cows produce less milk, Farmer John would like to count the number of such cows. Please help him.

N头牛在一个坐标轴上,每头牛有个高度。现给出一个距离值D。

如果某头牛在它的左边,在距离D的范围内,如果找到某个牛的高度至少是它的两倍,且在右边也能找到这样的牛的话。则此牛会感觉到不舒服。

问有多少头会感到不舒服。

Input

* Line 1: Two integers, N and D.

* Lines 2..1+N: Line i+1 contains the integers x(i) and h(i). The locations of all N cows are distinct.

Output

* Line 1: The number of crowded cows.

Sample Input

6 4
10 3
6 2
5 3
9 7
3 6
11 2

INPUT DETAILS: There are 6 cows, with a distance threshold of 4 for feeling crowded. Cow #1 lives at position x=10 and has height h=3, and so on.

Sample Output

2
OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.

HINT

Source

BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )的更多相关文章

  1. BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...

  2. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  3. 3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Sub ...

  4. 【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3314 一眼就是维护一个距离为d的单调递减队列... 第一次写.....看了下别人的代码... 这一题 ...

  5. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

  6. BZOJ : [Usaco2013 Nov]Crowded 单调队列

    正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...

  7. 【bzoj 1414】对称的正方形 单调队列+manacher

    Description Orez很喜欢搜集一些神秘的数据,并经常把它们排成一个矩阵进行研究.最近,Orez又得到了一些数据,并已经把它们排成了一个n行m列的矩阵.通过观察,Orez发现这些数据蕴涵了一 ...

  8. bzoj 1047 : [HAOI2007]理想的正方形 单调队列dp

    题目链接 1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2369  Solved: 1266[Submi ...

  9. BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )

    树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...

随机推荐

  1. HTML5 音频视频

    HTML5 视频和音频的 DOM 参考手册 HTML5 DOM 为 <audio> 和 <video> 元素提供了方法.属性和事件. 这些方法.属性和事件允许您使用 JavaS ...

  2. vs 2010 引用DLL 遇到问题

    1.新建项目→添加引用,提示:未能添加对dll的引用,请确保此文件可访问并且是一个有效的程序集或COM组件 解决方案:首先把DLL放system32中,然后,regsvr32 绝对路径注册一下: 如果 ...

  3. 两台linux机器时间同步

    Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time server的角色,让其他机器和他同步时间. 配 ...

  4. git配合tortoiseGit的基础使用

    转载自:http://www.cnblogs.com/ssor/archive/2012/02/04/2337823.html 一定要自己写出来才能牢记,所以我来写一下 git确实比svn好用的多了, ...

  5. 初识 python

    Python 语言介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python变化 python 2 和 python 3 1.1/2 等于0.5 2.print ...

  6. [LeetCode]题解(python):018-4Sum

    题目来源: https://leetcode.com/problems/4sum/ 题意分析: 这道题目和3Sum的题目类似,找出所有的4个数,使得这4个数等于target. 题目思路: 这道题做法和 ...

  7. 关于Mysql索引的笔记

    MySQL索引原理 索引目的 索引的目的在于提高查询效率,可以类比字典,如果要查“mysql”这个单词,我们肯定需要定位到m字母,然后从下往下找到y字母,再找到剩下的sql.如果没有索引,那么你可能需 ...

  8. Lake Counting(poj 2386)

    题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...

  9. Objective-C 链式编程思想

    链式编程思想 链式编程是什么 链式编程就是将调用多个方法用点语法连接起来,让代码更加简洁和可读性更高刚开始接触链式编程是Masonry,用起来真的非常爽 1 make.left.right.top.e ...

  10. BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题

    题目 1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer ...