USACO Gold 2022 Open - Apple Catching
Author: Qi Wang
Explanation
In order for a cow to catch an apple, it must arrive before the apple hits the number line. Let item represent the cow and item represent the apple. Therefore, to catch the apple, must be satisfied. After extracting the absolute value, we get these two inequalities.
We represent each item as a point . According to the inequalities above, in order for a cow to catch an apple, the point of the apple must be to its top-left. Thus we can sort in ascending order based off and greedily assign apples with larger values to each cow. To maximize the number caught, we must pair each cow with the lowest y-coordinate apples that still meet above conditions.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAX_N = 2e5;struct Item {int q; // type (cow or apple)int t; // time of entryint x; // positionint n; // amount
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!