题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314

题意:

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

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

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

题解:

  从左到右、从右到左两遍单调队列。

  单调性:

    (1)坐标x递增。

    (2)高度h递减。

  维护单调性:

    (1)从队首开始,所有与当前牛i的距离超过d的,以后都不会再用到。

    (2)从队尾开始,所有高度 <= 当前高度h[i]的,都不会再用到,因为当前牛i一定比前面的更优(又高又近)。

    (3)最后再将i压入队尾。

  每次判断一下之前最高的牛(队首)是不是h[i]的两倍,如果是则cnt[i]++。

  最后统计一下cnt[i] == 2的个数就好。

AC Code:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 50005 using namespace std; struct Data
{
int x;
int h;
Data(int _x,int _h)
{
x=_x;
h=_h;
}
Data(){}
friend bool operator < (const Data &a,const Data &b)
{
return a.x<b.x;
}
}; int n,d;
int head;
int tail;
int ans=;
int q[MAX_N];
int cnt[MAX_N];
Data dat[MAX_N]; void read()
{
cin>>n>>d;
for(int i=;i<n;i++)
{
cin>>dat[i].x>>dat[i].h;
}
} void solve()
{
sort(dat,dat+n);
memset(cnt,,sizeof(cnt));
head=;
tail=;
for(int i=;i<n;i++)
{
while(head<tail && dat[i].x-dat[q[head]].x>d) head++;
while(head<tail && dat[q[tail-]].h<=dat[i].h) tail--;
if(head<tail && dat[q[head]].h>=dat[i].h*) cnt[i]++;
q[tail++]=i;
}
head=;
tail=;
for(int i=n-;i>=;i--)
{
while(head<tail && dat[q[head]].x-dat[i].x>d) head++;
while(head<tail && dat[q[tail-]].h<=dat[i].h) tail--;
if(head<tail && dat[q[head]].h>=dat[i].h*) cnt[i]++;
q[tail++]=i;
}
for(int i=;i<n;i++)
{
if(cnt[i]==) ans++;
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}

最新文章

  1. iOS 开发学习资料整理(持续更新)
  2. 在 Github 上找「好东西」的方法
  3. angular2 递归导航菜单实现方式
  4. 控制反转容器&amp; 依赖注入模式 ---读感。
  5. runv kill 流程分析
  6. Android--自动搜索提示
  7. Javascript Date Format
  8. uboot官方FTP下载地址
  9. html img Src base64 图片显示
  10. Yii 2.0安装
  11. c++,初始化列表
  12. 十天学Linux内核之第七天---电源开和关时都发生了什么
  13. Q:算法(第四版)—第一章
  14. shell第四篇(下)
  15. 关于spring通知中propagation的7种配置《转载》
  16. 浅析HTTP代理原理--转
  17. showDialog 必须Stateful
  18. vsftpd 新增虚拟用户
  19. c# ref与out用法
  20. leetcode3

热门文章

  1. Java内存泄漏及分析
  2. bottle的几个小坑
  3. java 逻辑运算符 短路(条件操作)
  4. (二)关于jQuery
  5. openstack 用nova API 指定 compute node 创建 instance
  6. hdu 2814 Interesting Fibonacci
  7. Go开发常见陷阱
  8. TP 接收post请求使用框架自带函数I()防止注入
  9. 图像处理之log---log算子
  10. Spark源代码分析之六:Task调度(二)