Farmer John has recently expanded the size of his farm, so from the perspective of his cows it is effectively now infinite in size! The cows think of the grazing area of the farm as an infinite 2D grid of square "cells", each filled with delicious grass (think of each cell as a square in an infinite chessboard). Each of Farmer John's NN cows (1≤N≤10001≤N≤1000) starts out in a different cell; some start facing north, and some start facing east.
Every hour, every cow either
Over time, each cow therefore leaves a barren "rut" of empty cells behind her.
If two cows move onto the same grassy cell in the same move, they share the cell and continue moving in their respective directions in the next hour.
Farmer John isn't happy when he sees cows that stop grazing, and he wants to know who to blame for his stopped cows. If cow bb stops in a cell that cow aa originally ate, then we say that cow aa stopped cow bb. Moreover, if cow aa stopped cow bb and cow bb stopped cow cc, we say that cow aa also stopped cow cc (that is, the "stopping" relationship is transitive). Each cow is blamed in accordance with the number of cows she stopped. Please compute the amount of blame assigned to each cow -- that is, the number of cows she stopped.
The first line of input contains NN. Each of the next NN lines describes the starting location of a cow, in terms of a character that is either N (for north-facing) or E (for east-facing) and two nonnegative integers xx and yy (0≤x≤1090≤x≤109, 0≤y≤1090≤y≤109) giving the coordinates of a cell. All xx-coordinates are distinct from each-other, and similarly for the yy-coordinates.
To be as clear as possible regarding directions and coordinates, if a cow is in cell (x,y)(x,y) and moves north, she ends up in cell (x,y+1)(x,y+1). If she instead had moved east, she would end up in cell (x+1,y)(x+1,y).
Print NN lines of output. Line ii in the output should describe the blame assigned to the iith cow in the input.
6 E 3 5 N 5 3 E 4 6 E 10 4 N 11 1 E 9 2
0 0 1 2 1 0
In this example, cow 3 stops cow 2, cow 4 stops cow 5, and cow 5 stops cow 6. By transitivity, cow 4 also stops cow 6.
Problem credits: Brian Dean
[/hide]
(Analysis by Danny Mittal)
We can solve this problem by considering all pairs of a cow going east and a cow going north in order to determine which cow directly stops which other cow. Let's say the cow going east starts from (x,y)(x,y) and the cow going north starts from (u,v)(u,v). Their theoretical paths intersect if x<ux<u and v<yv<y, in which case they must intersect at (u,y)(u,y). This means that, assuming both cows reach (u,y)(u,y) (instead of being stopped earlier), the cow that reaches (u,y)(u,y) first will stop the other cow (if they both reach at the same time, as per the problem statement neither one is stopped).
Therefore, in order to determine the stopping relations, you might naively loop through all pairs of eastward cows and northward cows and see which cows reach the intersection point first. However, this doesn't account for the fact that either one may have been stopped earlier.
A clean way to deal with this is to sort all eastward cows by their yy and all northward cows by their xx, then loop through all pairs of eastward and northward cows in this order. We then keep track for each cow of whether we know it is stopped and the amount of cows that we know it has stopped (directly or indirectly).
The sorting guarantees that each northward cow will be checked against the eastward cows in increasing order of when the northward cow would reach their intersection point, and similarly for the eastward cows. Because of this, when we check a pair of cows neither of which we know has stopped yet, we can be sure that both will reach the intersection point and thus the earlier one must have stopped the later one. We also know that this means that the later cow can't now reach any more intersections than it already has, so the amount of cows it has stopped is final and we can add it, plus 11 for that cow itself, to the count for the earlier cow. Note that with other approaches, it may be necessary to run a second pass of essentially a recursive depth-first search to identify and count sizes of "connected components" of stopped cows, in order to assign blame counts appropriately.
The complexity of sorting is O(NlogN)O(NlogN), and looping through all pairs of northward and eastward cows is O(N2)O(N2), so the overall complexity is O(N2)O(N2). Solutions in O(N2logN)O(N2logN) would also be fast enough.
Java code:
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class StuckInARutSilver { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<Integer> eastCows = new ArrayList<>(); List<Integer> northCows = new ArrayList<>(); int n = in.nextInt(); int[] xs = new int[n]; int[] ys = new int[n]; for (int j = 0; j < n; j++) { if (in.next().charAt(0) == 'E') { eastCows.add(j); } else { northCows.add(j); } xs[j] = in.nextInt(); ys[j] = in.nextInt(); } eastCows.sort(Comparator.comparingInt(j -> ys[j])); northCows.sort(Comparator.comparingInt(j -> xs[j])); boolean[] isStopped = new boolean[n]; int[] amtStopped = new int[n]; for (int j : eastCows) { for (int k : northCows) { if (!isStopped[j] && !isStopped[k] && xs[k] > xs[j] && ys[j] > ys[k]) { if (xs[k] - xs[j] > ys[j] - ys[k]) { isStopped[j] = true; amtStopped[k] += 1 + amtStopped[j]; } else if (ys[j] - ys[k] > xs[k] - xs[j]) { isStopped[k] = true; amtStopped[j] += 1 + amtStopped[k]; } } } } for (int j = 0; j < n; j++) { System.out.println(amtStopped[j]); } } }
[/hide]
© 2025. All Rights Reserved. 沪ICP备2023009024号-1