3314: [Usaco2013 Nov]Crowded Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 86  Solved: 61
[Submit][Status]

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

题解:
单调队列维护可行域内的最大值。
1A是对我的一种补偿吗?T_T
代码:
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 100000+1000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct rec{int x,y;}a[maxn],q[maxn];
int n,m;
bool can1[maxn],can2[maxn];
inline bool cmp(rec a,rec b)
{
return a.x<b.x;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();
for1(i,n)a[i].x=read(),a[i].y=read();
sort(a+,a+n+,cmp);
int l=,r=;
for1(i,n)
{
while(l<=r&&q[r].y<a[i].y)r--;
q[++r]=a[i];
while(l<=r&&q[l].x<a[i].x-m)l++;
if(q[l].y>=a[i].y*)can1[i]=;
}
l=,r=;
for3(i,n,)
{
while(l<=r&&q[r].y<a[i].y)r--;
q[++r]=a[i];
while(l<=r&&q[l].x>a[i].x+m)l++;
if(q[l].y>=a[i].y*)can2[i]=;
}
int ans=;
for1(i,n)if(can1[i]&&can2[i])ans++;
printf("%d\n",ans);
return ;
}

BZOJ3314: [Usaco2013 Nov]Crowded Cows的更多相关文章

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

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

  2. 3314: [Usaco2013 Nov]Crowded Cows

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

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

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

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

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

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

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

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

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

  7. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit] ...

  8. BZOJ3016: [Usaco2012 Nov]Clumsy Cows

    3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 52[Submi ...

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

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

随机推荐

  1. hdu 1685 Booksort (IDA*)

    Booksort Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Tot ...

  2. Xcode7 国际化

    1.第一步 HaiTing_xcodeproj.png 2.第二不 HaiTing_xcodeproj 2.png 3.第三步 Localizable_strings.png 5第五步 ZLBMeVi ...

  3. 关于lower_bound()的用法--NYOJ 201作业题

    lower_bound它有三个参数, 第一个和第二个是给定区间起点和终点的指针,第三个参数是要查找的数,它的作用原理是在给定的区间中进行二分查找,这个二分区间是前开后闭的,他返回第一个大于等于它的函数 ...

  4. ViewPage 大圣归来 原生示例

    VP简介 android-support-v4.jar 是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而ViewPage就是其中之一,利用它,我 ...

  5. POJ2449

    #include<stdio.h> #include<iostream> #include<queue> #include<vector> using ...

  6. Android Activity间传值

    Android中不同的Activity之间的传值方式(Main为当前Activity,Login为目标Activity) 1.使用Intent Intent intent = new Intent(M ...

  7. c# 语句关键字

    1. yield 它表明一个方法,操作符或者get选择器里会出现迭代. 用yield return 同时返回每一个元素,  返回类型必须是IEnumerable, IEnumerable<T&g ...

  8. (转)XML CDATA是什么?

    解析数据 XML 解析器通常情况下会处理XML文档中的所有文本. 当XML元素被解析的时候,XML元素内部的文本也会被解析: <message>This text is also pars ...

  9. 开源CMS建站项目DNN研究与心得

    DNN (Dotnetnuke) 首先是开源的,并且采用BSD开源协议,也就是说你可以任意修改源代码.传播.作为商品出售,仅有的要求就是保留源代码中的版权文字,这就解决了我多年来的心病:我知道动网新闻 ...

  10. Oracle 11g随Redhat 5系统自动启动与关闭的设置方法

    最近,ORACLE系统基本调试通过,是时候设置ORACLE随RHEL自动启动与关闭服务的时候了,之所以把这个任务放在最后来做,是因为我觉得这个应该不会很难,但真正实施起来,还是遇到了个不小的障碍:写好 ...