第一次写单调队列太垃圾。。。

左右各扫一遍即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define N 50010
 6 using namespace std;
 7 struct data
 8 {
 9     int x,h;
10 }a[N],q[N];
11 int n,m,l,r;
12 int ok1[N],ok2[N];
13 inline int read()
14 {
15     int f=1,ans=0;
16     char c;
17     while (!isdigit(c=getchar())) if (c=='-') f=-1;
18     ans=c-'0';
19     while (isdigit(c=getchar())) ans=ans*10+c-'0';
20     return ans*f;
21 }
22 bool cmp(data a,data b) {return a.x<b.x;}
23 int main()
24 {
25     n=read(); m=read();
26     for (int i=1;i<=n;i++) a[i].x=read(),a[i].h=read();
27     sort(a+1,a+n+1,cmp);
28     l=1; r=0;
29     for (int i=1;i<=n;i++)
30     {
31         while (l<=r && q[r].h<a[i].h) r--;
32         q[++r]=a[i];
33         while (l<=r && q[l].x<a[i].x-m) l++;
34         if (q[l].h>=2*a[i].h) ok1[i]=1;
35     }
36     l=1; r=0;
37     for (int i=n;i>=1;i--)
38     {
39         while (l<=r && q[r].h<a[i].h) r--;
40         q[++r]=a[i];
41         while (l<=r && q[l].x>a[i].x+m) l++;
42         if (q[l].h>=2*a[i].h) ok2[i]=1;
43     }
44     int ans=0;
45     for (int i=1;i<=n;i++)
46         if (ok1[i]&&ok2[i]) ans++;
47     printf("%d\n",ans);
48     return 0;
49 }

View Code


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

Silver

转载于:https://www.cnblogs.com/DMoon/p/5422650.html

【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列相关推荐

  1. 单调队列(P3088 Crowded Cows S)

    题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i ...

  2. [DP/单调队列]BZOJ 2059 [Usaco2010 Nov]Buying Feed 购买饲料

    首先我想吐槽的是题目并没有表明数据范围... 这个题目 DP方程并不难表示. dp[i][j]表示前i个地点携带了j个货物的最小花费 dp[i][j] = dp[i-1][k] + (j-k) * c ...

  3. Buying Feed, 2010 Nov (单调队列优化DP)

    约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里, 这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算 ...

  4. Crowded Cows(洛谷P3088)

    [USACO13NOV]CrowdedCowsS[USACO13NOV]Crowded Cows S[USACO13NOV]CrowdedCowsS 传送门 思路 先看数据, 对于 202020 %的 ...

  5. P3088 [USACO13NOV]CROWDED COWS S

    https://www.luogu.com.cn/problem/P3088 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazin ...

  6. [USACO13NOV]挤奶牛Crowded Cows(洛谷 P3088)

    题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i ...

  7. 【POJ - 2373】Dividing the Path(单调队列优化dp)

    题干: Farmer John's cows have discovered that the clover growing along the ridge of the hill in his fi ...

  8. POJ 3250 Bad Hair Day【单调队列】

    Description Some of Farmer John'sNcows (1 ≤N≤ 80,000) are having a bad hair day! Since each cow is s ...

  9. HDU 4122 Alice's mooncake shop 单调队列优化dp

    Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

最新文章

  1. java程序设计实验报告_JavA程序设计实验报告.doc
  2. Handler消息机制(十):HandlerThread源码解析
  3. Linux中的popen-pclose
  4. python3记录(1) - 内置函数
  5. 从数据平台到报表开发,我靠这个零代码报表工具,转行后月薪3W
  6. Linux格式化分区报错Could not start /dev/sda No such file or directory 解决办法
  7. art template模板中修改时间格式
  8. 御剑飞行扫描后门加上burpsuite字典树爆破
  9. mysql netbeans_使用Netbeans操作MySQL数据库
  10. 春暖花开,微信公众号图文排版素材礼包已到!
  11. Typora也能做思维导图?做笔记的又一个绝佳选择?
  12. android手机内存使用情况分析
  13. 例题9-27 方块消除 UVa10559
  14. pytorch加载自己的数据集,数据集载入-视频合集
  15. 零基础计算机英语教程,零基础学英语语法的方法
  16. 完美国际2找回老账号服务器,我告诉大家一个找回账号的办法
  17. 【JavaScript】浏览器
  18. 【图片新闻】五角大楼的国防创新部DIU正在规划建设一个未来机器人空间站
  19. Ajax怎么获取天气,Ajax获取全国天气预报的API数据
  20. 三角形形态判断 循环计算e Python123题解

热门文章

  1. 手机怎么下载python并安装-Python入门【1】Python下载安装,这几步你要了解
  2. python画图完整代码-Python科学画图代码分享
  3. python自动化办公都能做什么菜-Python自动化开发学习之三级菜单制作
  4. python解释器是什么-python的解释器是什么?
  5. python资料百度网盘-python自动保存百度盘资源到百度盘中的实例代码
  6. python上海培训哪里比较好-python培训机构上海哪里好?
  7. python是什么公司开发的软件-python适合开发桌面软件吗?
  8. 有必要学python吗-让孩子学Python编程有必要吗
  9. python中处理日期和时间的标准模块是-2019python常见的170道面试题解析
  10. 用python画烟花-用python实现漂亮的烟花demo