Comfortable Cows
InterviewTime limit1sMemory limit512 MB
Cows arrive one at a time on a grid; after each arrival, report how many cows have exactly three occupied orthogonal neighbors.
- Level
Medium6 of 10
- Topics
- Implementation, Hash map, Simulation, Array
- Solved
- No attempts yet
Problem
Farmer John's pasture can be regarded as a large 2D grid of square "cells" (picture a huge chessboard). Initially, the pasture is empty.
Farmer John will add () cows to the pasture one by one. The th cow will occupy a cell that is distinct from the cells occupied by all other cows ().
A cow is said to be "comfortable" if it is horizontally or vertically adjacent to exactly three other cows. Farmer John is interested in counting the comfortable cows on his farm. For each in the range , output the total number of comfortable cows after the th cow is added to the pasture.
Input
The first line contains a single integer . Each of the next lines contains two space-separated integers, indicating the coordinates of a cow's cell. It is guaranteed that all these cells are distinct.
Output
The th line of output should contain the total number of comfortable cows after the first cows are added to the pasture.