tinycodinglm / misc /misc-0173.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
5.59 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing...

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution.

User

Note: The time limit for this problem is 4s, two times the default.

Somebody has been grazing in Farmer John's $(1 \le G \le 10^5)$
private gardens! Using his expert forensic knowledge, FJ has been able to
determine the precise time each garden was grazed. He has also determined that
there was a single cow that was responsible for every grazing incident.

In response to these crimes each of FJ's $N$ $(1 \le N \le 10^5)$
cows have provided an alibi that proves the cow was in a specific location at a
specific time. Help FJ test whether each of these alibis demonstrates the cow's
innocence.

A cow can be determined to be innocent if it is impossible for her to have
travelled between all of the grazings and her alibi. Cows travel at a rate of 1
unit distance per unit time.

INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input will contain $G$ and $N$ separated by a space.

The next $G$ lines contain the integers $x$, $y$, and $t$
$(-10^9 \le x, y \le 10^9; 0 \le t \le 10^9)$ separated by a space
describing the location and time of the grazing. It will always be possible for
a single cow to travel between all grazings.

The next $N$ lines contain $x$, $y$, and $t$
$(-10^9 \le x, y \le 10^9; 0 \le t \le 10^9)$ separated by a space
describing the location and time of each cow's alibi.

OUTPUT FORMAT (print output to the terminal / stdout):
Output a single integer: the number of cows with alibis that prove their
innocence.

SAMPLE INPUT:
2 4
0 0 100
50 0 200
0 50 50
1000 1000 0
50 0 200
10 0 170
SAMPLE OUTPUT:
2

There were two grazings; the first at $(0, 0)$ at time $100$ and the
second at $(50, 0)$ at time $200$.

The first cow's alibi does not prove her innocence. She has just enough time to
arrive at the first grazing.

The second cow's alibi does prove her innocence. She is nowhere near any of the
grazings.

Unfortunately for the third cow, being at the scene of the crime does not prove
innocence.

Finally, the fourth cow is innocent because it's impossible to make it from her
alibi to the final grazing in time.

SCORING:
Inputs 2-4: $1 \le G, N \le 10^3$. Also, for both the fields and alibis,
$-10^6 \le x, y \le 10^6$ and $0 \le t \le 10^6$. Inputs 5-11: No additional constraints.

Problem credits: Mark Gordon

Assistant

import bisect

def main():
import sys
input = sys.stdin.read().split()
ptr = 0
G = int(input[ptr])
ptr += 1
N = int(input[ptr])
ptr += 1

markdown
gardens = []
for _ in range(G):
    x = int(input[ptr])
    ptr += 1
    y = int(input[ptr])
    ptr += 1
    t = int(input[ptr])
    ptr += 1
    gardens.append((t, x, y))

# Sort gardens by time
gardens.sort()
T = [g[0] for g in gardens]

result = 0
for _ in range(N):
    x = int(input[ptr])
    ptr += 1
    y = int(input[ptr])
    ptr += 1
    t_alibi = int(input[ptr])
    ptr += 1

    if t_alibi < T[0]:
        # Check with first garden
        a_t, a_x, a_y = gardens[0]
        dx = x - a_x
        dy = y - a_y
        dt = a_t - t_alibi
        distance_sq = dx * dx + dy * dy
        if distance_sq <= dt * dt:
            continue
        else:
            result += 1
    elif t_alibi > T[-1]:
        # Check with last garden
        a_t, a_x, a_y = gardens[-1]
        dx = x - a_x
        dy = y - a_y
        dt = t_alibi - a_t
        distance_sq = dx * dx + dy * dy
        if distance_sq <= dt * dt:
            continue
        else:
            result += 1
    else:
        # Bisect to find the correct index
        idx = bisect.bisect_right(T, t_alibi) - 1
        k = idx
        if k >= len(gardens) - 1:
            # Alibi time is exactly the last garden's time
            a_t, a_x, a_y = gardens[k]
            dx = x - a_x
            dy = y - a_y
            distance_sq = dx * dx + dy * dy
            if distance_sq == 0:
                continue
            else:
                result += 1
        else:
            A = gardens[k]
            B = gardens[k+1]
            # Check between A and B
            dx_A = x - A[1]
            dy_A = y - A[2]
            dt_A = t_alibi - A[0]
            dist_sq_A = dx_A * dx_A + dy_A * dy_A
            valid_A = (dist_sq_A <= dt_A * dt_A)
            
            dx_B = x - B[1]
            dy_B = y - B[2]
            dt_B = B[0] - t_alibi
            dist_sq_B = dx_B * dx_B + dy_B * dy_B
            valid_B = (dist_sq_B <= dt_B * dt_B)
            
            if valid_A and valid_B:
                continue
            else:
                result += 1
print(result)

if name == "main":
main()