You are on page 1of 2

11/21/13

Contest Results

Anal sis: Crowded Cows, b Brian Dean


The e a e e e a a
e hi
b e i O(N g N) i e i g a " ee i e" a
ach. A
G d ' c de be , e ch
i i
he c
b
i i , he
ca h gh hi
ai ai i g he heigh
fa c
i he a ge -d
,a da c
i he a ge
+d, he e
he c e c
d i g
ca . T
ai ai he heigh f he c
i he e
idi g i d
a i i
e e a e (i.e., a ba a ced bi a
ea ch ee). A e i i each c , e ca e if i
e i g f he a i
heigh i b h i d
; if each a i
i
e ha
ice he heigh
he c
i c
ded.
A e a i e , e ca ca
ha he
e
ee i e i
c , he
ii
f ha c
ee i e i i a c
( a
f
i g (i a STL e ,
he
ii
fa c
a
ii
a e i hi he a

h
i Ma
de i g b
i he
ii
f
, e ca
e ei he
i c
ded b
f he c e c ,

he c
i dec ea i g de f heigh , i g a ai f
ee i e ha
ei c
e
a a a
ice he heigh a he
e
ee i e. Whe e e he
e
ee i e i i a
i i e ed i
a e da a
c e (i.e., a ba a ced bi a
ea ch ee). Whe he
e
,a
ii
), e e hi
c e f he
ii
i
edia e
ecedi g a d
f e a e, e c d e he
e _b
d e h d d hi ). The da a
c e c ai
ea
ice he heigh f he c e c ,
if he edece
a d cce
ba ed
he
ge -d
+d, he he c e c
i c
ded.

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;
int main() {
freopen("crowded.in", "r", stdin);
freopen("crowded.out", "w", stdout);
int N, D;
cin >> N >> D;
vector<pair<int, int> > A(N);
for(int i = 0; i < N; i++)
cin >> A[i].first >> A[i].second;
sort(A.begin(), A.end());
int result = 0;
multiset<int> X, Y;
int j = 0, k = 0;
for(int i = 0; i < N; i++) {
while(k < N && A[k].first <= A[i].first + D) {
Y.insert(A[k++].second);
while(A[j].first + D < A[i].first) {
X.erase(X.find(A[j++].second));
X.insert(A[i].second);
if (*--X.end() >= 2 * A[i].second &&
*--Y.end() >= 2 * A[i].second) {
result++;

Y.erase(Y.find(A[i].second));
.usaco.org/current/data/sol_cro ded.html

1/2

11/21/13

Contest Results

cout << result << endl;


return 0;

.usaco.org/current/data/sol_cro ded.html

2/2

You might also like