【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& ...
随机推荐
- Ubuntu 下安装adobe reader
ctrl+alt+t打开终端 wget ftp://ftp.adobe.com/pub/adobe/reader/unix/9.x/9.5.5/enu/AdbeRdr9.5.5-1_i386linux ...
- Android布局基础
布局过程的含义 布局过程,就是程序在运行时利用布局文件的代码来计算出实际尺寸的过程. 布局过程的工作内容 两个阶段:测量阶段和布局阶段. 测量阶段:从上到下递归地调用每个 View 或者 ViewGr ...
- 生成批量删除多个表sql语句
--批量删除多个表 select 'drop table ' +name from sysobjects where type = 'U' order by name
- 【Python 数据分析】pandas数据导入
导入CSV文件数据 环境 C:\Users\Thinkpad\Desktop\Data\信息表.csv 语法 pd.read_csv(filename):从CSV文件导入数据 实现代码 import ...
- Github 入门基本操作
翻译自:https://guides.github.com/activities/hello-world/ 文章概述: 什么是GitHub? 创建一个存储库 创建一个分支 做出承诺 打开拉请求 合并拉 ...
- 转:maven3常用POM属性及Settings属性介绍
原文:http://blog.csdn.net/lgm277531070/article/details/6922645 A.pom.xml属性介绍: project: pom的xml根元素. par ...
- R语言的if-else
语法问题,类似于for函数使用时必须把"{"放在当前行,而不可以独占一行.R的特色. 具体你的这个问题可以尝试 x <- 5 if (x>0){ print(" ...
- 解决 adb devices :???????????? no permissions 方法
- IE6—在链接click事件的响应函数中发送jsonp请求不生效
$("#link").click(function(){ $.ajax({ type: 'GET', dataType: 'jsonp', ...
- LAMP架构二
安装PHP7 1.查看php配置文件信息(phpinfo),php有两个配置文件开发环境和生产环境 [root@localhost php-5.6.30]# /usr/local/php/bin/ph ...