http://www.lydsy.com/JudgeOnline/problem.php?id=3314

一眼就是维护一个距离为d的单调递减队列。。。

第一次写。。。。。看了下别人的代码。。。

这一题只需要维护距离为d的最大,然后判断最大的是否大于等于自己高度*2

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j] << ' '; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i] << ' '; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005;
int q[N], front, tail, n, d, mx[2][N];
struct dat { int x, h; }a[N];
bool cmp(const dat &a, const dat &b) { return a.x<b.x; }
void work(int *arr) {
front=tail=0;
for1(i, 1, n) {
while(front!=tail && abs(a[i].x-a[q[front]].x)>d) ++front;
if(front!=tail) arr[i]=q[front];
while(front!=tail && a[i].h>a[q[tail-1]].h) --tail;
q[tail++]=i;
}
} int main() {
read(n); read(d);
for1(i, 1, n) read(a[i].x), read(a[i].h);
sort(a+1, a+1+n, cmp);
work(mx[0]);
for1(i, 1, n) if(i<n-i+1) swap(a[i], a[n-i+1]);
work(mx[1]);
int ans=0;
for1(i, 1, n) if(a[mx[1][i]].h>=(a[i].h<<1) && a[n-mx[0][n-i+1]+1].h>=(a[i].h<<1)) ++ans;
print(ans);
return 0;
}

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( 单调队列 )

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

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

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

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

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

  4. 3314: [Usaco2013 Nov]Crowded Cows

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

  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. 聊聊iOS中网络编程长连接的那些事

    1.长连接在iOS开发中的应用 常见的短连接应用场景:一般的App的网络请求都是基于Http1.0进行的,使用的是NSURLConnection.NSURLSession或者是AFNetworking ...

  2. selenium 问题:加了显性等待后,操作元素依然出错

    背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待元素出现.结果屡屡出问题.元素默认是隐藏的,导致等待过早的就结 ...

  3. iOS APP 上传

    原地址:http://www.cnblogs.com/uvsjoh/archive/2012/11/14/2769739.html 流程:1 开发好要发布的程序 -- 需要在程序中包含符合要求规格的i ...

  4. c++11 学习

    #include <iostream> // std::cout #include <functional> // std::ref #include <thread&g ...

  5. TX2 ROS IDE开发环境配置

    参考资料: http://www.mamicode.com/info-detail-1663827.html    基于Qt搭建ROS开发环境 https://blog.csdn.net/sbtxg/ ...

  6. Android平台上直接物理内存读写漏洞的那些事

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 通过mmap直接操作物理内存的漏洞应该算是比較常见的一类漏洞了,在2012年.2 ...

  7. 【Shiro】Apache Shiro架构之权限认证(Authorization)

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...

  8. STM32的IO口灌入电流和输出驱动电流最大是多少?

    最大可以输出8mA,灌入20mA,但要保证所有进入芯片VDD的电流不能超过150mA,同样所有从VSS流出的电流也不能超过150mA. 详细请看STM32的数据手册中的相关内容. 例如,STM32F1 ...

  9. 【剑指offer】Q17:合并两个排序的链表

    def Merge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 psuhead = Lis ...

  10. Sublime Text 2 入门与总结

    Sublime Text 2 入门与总结   首语 :    考完试,但又没什么兴趣做课程设计,蛋疼的弄点软件入门的介绍,希望给各位还在吃香蕉的程序猿带来一点启示...   代码编辑器,就像武侠中的武 ...