Mice (Codeforces 76B)
Tags: Greedy, two pointers

Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and there is still a piece of cheese in it, they eat it and become well-fed. Each mice that reaches this point after that remains hungry. Moving speeds of all mice are equal.If there are several ways to choose closest pieces then mice will choose it in a way that would minimize the number of hungry mice. To check this theory scientists decided to conduct an experiment. They located N mice and M pieces of cheese on a cartesian plane where all mice are located on the line y = Y0 and all pieces of cheese — on another line y = Y1. To check the results of the experiment the scientists need a program which simulates the behavior of a flock of hungry mice.Write a program that computes the minimal number of mice which will remain hungry, i.e. without cheese.

Input: The first line of the input contains four integer numbers N (1 ≤ N ≤ 105), M (0 ≤ M ≤ 105), Y0 (0 ≤ Y0 ≤ 107), Y1 (0 ≤ Y1 ≤ 107, Y0 ≠ Y1). The second line contains a strictly increasing sequence of N numbers — x coordinates of mice. Third line contains a strictly increasing sequence of M numbers — x coordinates of cheese. All coordinates are integers and do not exceed 107 by absolute value.

Output: The only line of output should contain one number — the minimal number of mice which will remain without cheese.

Note: All the three mice will choose the first piece of cheese. Second and third mice will eat this piece. The first one will remain hungry, because it was running towards the same piece, but it was late. The second piece of cheese will remain uneaten.

Sample Inputs:
3 2 0 2
0 1 3
2 5

Sample Outputs:
1

Solution (Python):
(n, m, y0, y1) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = 0
vis = [0] * m
dis = [0] * m
for i in range(n):
    while pos < m - 1 and abs(b[pos] - a[i]) > abs(b[pos + 1] - a[i]):
        pos += 1
    if pos < m - 1 and abs(b[pos] - a[i]) == abs(b[pos + 1] - a[i]):
        if not vis[pos]:
            vis[pos] = 1
            dis[pos] = abs(a[i] - b[pos])
        elif dis[pos] == abs(a[i] - b[pos]):
            vis[pos] += 1
        else:
            dis[pos + 1] = abs(a[i] - b[pos + 1])
            vis[pos + 1] = 1
    elif not vis[pos]:
        vis[pos] = 1
        dis[pos] = abs(a[i] - b[pos])
    elif dis[pos] == abs(a[i] - b[pos]):
        vis[pos] += 1
    elif dis[pos] > abs(a[i] - b[pos]):
        vis[pos] = 1
        dis[pos] = abs(a[i] - b[pos])
print(n - sum(vis))

Solution (Python):
def distance(x, y):
    return abs(x - y)
(n, m, y0, y1) = map(int, input().split())
mouse = list(map(int, input().split()))
cheeses = list(map(int, input().split()))
i = 0
j = 0
res = 0
mouse.append(float('inf'))
cheeses.append(float('inf'))
prevJ = -1
for i in range(n):
    while distance(mouse[i], cheeses[j]) > distance(mouse[i], cheeses[j + 1]):
        j += 1
    if prevJ < j or (0 < i and distance(mouse[i - 1], cheeses[j]) == distance(mouse[i], cheeses[j])):
        res += 1
    elif j + 1 < m and distance(mouse[i], cheeses[j + 1]) == distance(mouse[i], cheeses[j]):
        res += 1
        j += 1
    prevJ = j
print(n - res)

Solution (Python):
(n, m, y0, y1) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = 0
vis = [0] * m
dis = [0] * m
for i in range(n):
    while pos < m - 1 and abs(b[pos] - a[i]) > abs(b[pos + 1] - a[i]):
        pos += 1
    if pos < m - 1 and abs(b[pos] - a[i]) == abs(b[pos + 1] - a[i]):
        if not vis[pos]:
            vis[pos] = 1
            dis[pos] = abs(a[i] - b[pos])
        elif dis[pos] == abs(a[i] - b[pos]):
            vis[pos] += 1
        else:
            dis[pos + 1] = abs(a[i] - b[pos + 1])
            vis[pos + 1] = 1
    elif not vis[pos]:
        vis[pos] = 1
        dis[pos] = abs(a[i] - b[pos])
    elif dis[pos] == abs(a[i] - b[pos]):
        vis[pos] += 1
    elif dis[pos] > abs(a[i] - b[pos]):
        vis[pos] = 1
        dis[pos] = abs(a[i] - b[pos])
print(n - sum(vis))