题目链接: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();
}

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

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

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

  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. Android 进程间通信——AIDL

    代码地址如下:http://www.demodashi.com/demo/12321.html 原文地址:http://blog.csdn.net/vnanyesheshou/article/deta ...

  2. CSS3 background属性

    background: #00FF00 url(bgimage.gif) no-repeat fixed top; background 简写属性在一个声明中设置所有的背景属性. 可以设置如下属性: ...

  3. LeetCode HashTable 30 Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. View的滚动原理简单解析

    一直对View的滚动了解的不深,说明确了吧也能说出个所以然来,所以我就花了点时间做了一个小小的总结,言归正传,view的滑动分为下面三种: 1)View本身不滚动,指滚动View的内容,这也是View ...

  5. Bootstrap学习速查表(一) 理论基础

    参考网站http://www.bootcss.com/ 第一步,起步,引入基本样式 <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="st ...

  6. 宜信开源|微服务任务调度平台SIA-TASK入手实践

    引言 最近宜信开源微服务任务调度平台SIA-TASK,SIA-TASK属于分布式的任务调度平台,使用起来简单方便,非常容易入手,部署搭建好SIA-TASK任务调度平台之后,编写TASK后配置JOB进行 ...

  7. 浅谈iOS中MVVM的架构设计与团队协作【转载】

    今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

  8. 已知某公司总人数为W,平均年龄为Y岁(每年3月末计算,同时每年3月初入职新人),假设每年离职率为x,x>0&&x<1,每年保持所有员工总数不变进行招聘,新员工平均年龄21岁。 从今年3月末开始,请实现一个算法,可以计算出第N年后公司员工的平均年龄。(最后结果向上取整)。

    // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...

  9. mongodb的启动参数--quiet

    ”mongo群友在群里问了个问题,问的是--quiet启动参数如何用的? 如何理解安静的输出?“ 看到这个问题,之前看过--quiet这个参数,没有认真研究过,也没在生产中使用过. 在mongodb启 ...

  10. python scrapy爬虫框架

    http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html scrapy 提取html的标签内容 from scrapy.selec ...