【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

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

原文地址:https://www.cnblogs.com/iwtwiioi/p/3976958.html