【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)
http://www.lydsy.com/JudgeOnline/problem.php?id=3314
一眼就是维护一个距离为d的单调递减队列。。。
第一次写。。。。。看了下别人的代码。。。
这一题只需要维护距离为d的最大,然后判断最大的是否大于等于自己高度*2
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j] << ' '; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i] << ' '; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005;
int q[N], front, tail, n, d, mx[2][N];
struct dat { int x, h; }a[N];
bool cmp(const dat &a, const dat &b) { return a.x<b.x; }
void work(int *arr) {
front=tail=0;
for1(i, 1, n) {
while(front!=tail && abs(a[i].x-a[q[front]].x)>d) ++front;
if(front!=tail) arr[i]=q[front];
while(front!=tail && a[i].h>a[q[tail-1]].h) --tail;
q[tail++]=i;
}
} int main() {
read(n); read(d);
for1(i, 1, n) read(a[i].x), read(a[i].h);
sort(a+1, a+1+n, cmp);
work(mx[0]);
for1(i, 1, n) if(i<n-i+1) swap(a[i], a[n-i+1]);
work(mx[1]);
int ans=0;
for1(i, 1, n) if(a[mx[1][i]].h>=(a[i].h<<1) && a[n-mx[0][n-i+1]+1].h>=(a[i].h<<1)) ++ans;
print(ans);
return 0;
}
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
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
OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.
HINT
Source
【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)的更多相关文章
- BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )
		
从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...
 - BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列
		
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...
 - 【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 ...
 - 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& ...
 
随机推荐
- Office WPS如何让页与页之间不相互影响
			
在一个页面结束的位置点击插入-分页符,完了之后测试按回车下一页的内容有没有跟着往下跑,如果还是跟着往下跑的,再插入一次分页符,一般插入多次之后就不会跟着跑了,但是插的太多会有空白页面 测试按回车, ...
 - java开发中的一些概念名词
			
1. JavaBeans JavaBean是符合某种规范的Java组件,也就是Java类.它必须满足如下规范: 1)必须有一个零参数的默认构造函数 2)必须有get和set方法,类的字段必须通过get ...
 - 算法笔记_083:蓝桥杯练习 合并石子(Java)
			
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 在一条直线上有n堆石子,每堆有一定的数量,每次可以将两堆相邻的石子合并,合并后放在两堆的中间位置,合并的费用为两堆石子的总数.求把所有石子 ...
 - iOS-格式化金额,三位一逗号
			
代码地址如下:http://www.demodashi.com/demo/11244.html 项目版本更新迭代中, 新增需求: 所有金额必须用标准会计表示方式(¥94,862.57). 而之前金额展 ...
 - CentOS修复grub
			
grub启动项损坏无法进入系统. 进入grub模式(可借助安排盘rescue后在shell中输入grub). 一: 通过下面三个命令中的一个.找到正确的grub位置. 1. find ...
 - hudson status
			
http://sin90lzc.iteye.com/blog/1072123在eclipse上显示hudson构建状态 update site:http://eclipse-plugin-potpou ...
 - 修改easyui panel 默认样式
			
有这么个需求需要修改easyui panel头部中的背景色.于是根据panel中的最终被浏览器解析出来的类名,直接修改这个css样式,设置backgroud-color这个属性,发现不管用. 于是,就 ...
 - EF、MySQL、MVC、WebAPI2 swagger 集成
			
“好记星不如烂笔头",这句话一直伴随我多年,想当年还是我语文老师常用的口头禅. 时间一晃3年过去了.以前只是记得自己去看别人的博客园.时间不久自己也开通了博客园来玩玩.顺便吧自己学的记录下来 ...
 - python --闭包学习
			
闭包概念: Closure:内部函数中对enclosing作用域的变量进行引用 enclosing作用域:函数内部与内嵌函之间 范例1: #coding:utf-8 def set_passline( ...
 - 通过使用JanusGraph索引提高性能
			
翻译整理:纪玉奇 Extending JanusGraph Server JanusGraph支持两种类型的索引:graph index和vertex-centric index.graph inde ...