BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列
题目链接: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:单调队列的更多相关文章
- BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )
从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- 3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 111 Solved: 79[Sub ...
- 【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)
http://www.lydsy.com/JudgeOnline/problem.php?id=3314 一眼就是维护一个距离为d的单调递减队列... 第一次写.....看了下别人的代码... 这一题 ...
- BZOJ3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 61[Subm ...
- BZOJ : [Usaco2013 Nov]Crowded 单调队列
正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...
- 【bzoj 1414】对称的正方形 单调队列+manacher
Description Orez很喜欢搜集一些神秘的数据,并经常把它们排成一个矩阵进行研究.最近,Orez又得到了一些数据,并已经把它们排成了一个n行m列的矩阵.通过观察,Orez发现这些数据蕴涵了一 ...
- bzoj 1047 : [HAOI2007]理想的正方形 单调队列dp
题目链接 1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2369 Solved: 1266[Submi ...
- BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )
树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...
随机推荐
- hibernate 数据缓存
http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html,
- vue.js+koa2项目实战(一)创建项目和elementUI配置
前端采用vuex+element-ui: 后端采用koa2+restfulAPI+sequlize: (一)项目介绍 宠物社区 1.社区 2.好友 3.说说 4.宠粮 5.健康 (二)项目框架 1.V ...
- 重读金典------高质量C编程指南(林锐)-------第五章 常量
5.1 为什么需要常量 1)为了便于用户理解,增加程序的可读性. 2)在程序的很多地方都用到同一个常量,用某一个宏常量来定义可以减少错误. 规则:尽可能的使用含义直观明确的常量来表示程序中多次出现的 ...
- javascript判断智能终端信息
< script type = "text/javascript" > /* * 智能机浏览器版本信息: * */ var browser = { versions: ...
- 转:DDR中端接技术基本概念
DDR中端接技术基本概念 版权声明:转载请注明出处:http://blog.csdn.net/lg2lh https://blog.csdn.net/lg2lh/article/details/90 ...
- neural network and deep learning笔记(1)
neural network and deep learning 这本书看了陆陆续续看了好几遍了,但每次都会有不一样的收获. DL领域的paper日新月异.每天都会有非常多新的idea出来,我想.深入 ...
- Spring Data JPA 事务锁
1.概述 在本快速教程中,我们将讨论在Spring Data JPA中为自定义查询方法和预定义存储库的CRUD方法启用事务锁, 我们还将查看不同的锁类型并设置事务锁超时. 2.锁类型 JPA定义了两种 ...
- JS 的引用赋值与传值赋值
这个问题说大不大说小不小,如果你有幸踩了这个坑,一定会找这篇文章,哈哈~ 现说一下JS数字的类型:基本类型和引用类型 先看下下面两个栗子: var a = 30; var b = a; a = 20; ...
- 全命令行手写MapReduce并且打包运行
主要要讲的有3个 java中的package是干啥的? 工作了好几年的都一定真正理解java里面的package关键字,这里在写MapReduce需要进行打包的时候突然发现命令行下打包运行居然不会了, ...
- JAVA进阶-多线程(2)
堵塞队列: 1)BlockingQueue该接口提供了: add()/remove() 假设当队列没有数据,从队列中取数据;或者队列中数据已满, 向队列中加入数据;则会抛出异常. put()/take ...