Explanation
Let's analyze the last test case in detail.
We denote a "desirable location" of a cow as a location where a cow of that species will be checked by the bovine veterinarian.
We denote a "contribution" as one instance of a cow being moved to a desirable location.
Note that a contribution can only be achieved in one of two ways.
The first way is if a cow was not in a desirable location, and the cow was moved to a desirable location during an operation.
The second way is if a cow was already in a desirable location, and the cow was not moved during an operation.
Let's consider how to calculate the contributions created strictly by the first scenario.
Let a cow at location try to reach a desirable location at location , where .
The most intuitive way to achieve this is by setting to and to , so that after the operation, is moved to .
We observe that the same is true if we set to and to .
Thus, the amount of contributions that can be made from a cow at location and desirable location at location is limited by the minimum distance to either end of the order, as after that or would violate the given constraints for a valid operation.
In a 0-indexing system, the contribution from and can be written as .
Now we consider how to calculate contributions created strictly by the second scenario.
Let a cow at position already be at a desirable location.
The amount of contributions created is equal to the number of operations that do not involve . In the problem, we are given that the number of operations that can occur between and can be written as
Thus, the amount of contributions created from a cow at location at a desirable location can be written as the sum of all the possible operations before and after :
We can implement this by iterating through each cow to determine how many contributions each cow in the initial order makes.
As each cow either is or isn't already at a desirable location, the second scenario is handled with a simple check if the cow is in a desirable location, then applying the formula described above.
However, for the first scenario, there may be multiple desirable locations a cow could move to.
Let's denote a cow and a desirable location that can create contributions as described in the first scenario as a pair .
If we naively checked each pair, this would take time per cow, as in the worst case scenario, each pair of locations is viable.
Let's denote a desirable location's minimum distance to an end as and a cow's minimum distance to an end as , where denotes the cow's location and denotes the desired location.
For a given cow, consider all desirable locations that it can pair with. Each pair contributes contributions. Thus, to speed up processing, we want to separate the pairs which the given cow is a part of into two groups: ones where contributions are limited by because , and ones where contributions are limited by because . We can partition them by creating a sorted array of all , then binary searching over this array to determine the index where . Before this index, contributions will be limited by , and after this index, contributions will be limited by .
However, this still results in time, since we still have to sum up all contributions at the end.
We can optimize this by implementing a prefix sum array that sums the indices of the array, so we can find the sum of valid from the start of a list to any point in time. Then, all contributions limited by can be found by multiplying by the number of pairs limited.
Thus, the formula for the contributions provided by the first scenario can be found as written below, given is the point where becomes less than :
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int main() {ios::sync_with_stdio(0);cin.tie(0);int num_cows;cin >> num_cows;
Python
import bisectnum_cows = int(input())initial_order = [int(x) for x in input().split()]ideal_order = [int(x) for x in input().split()]# Map a cow's species to an array of minimum distances to the edge for that species in the ideal orderingedge_arrays = dict()for i in range(num_cows):
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!