Table of Contents

ExplanationImplementation

Official Analysis (C++)

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.

firstscenario

The second way is if a cow was already in a desirable location, and the cow was not moved during an operation.

secondscenario

Let's consider how to calculate the contributions created strictly by the first scenario.

Let a cow at location ii try to reach a desirable location at location jj, where i<ji < j.

The most intuitive way to achieve this is by setting ll to ii and rr to jj, so that after the operation, ii is moved to jj.

firstscenarioexact

We observe that the same is true if we set ll to i1i - 1 and rr to j+1j + 1.

firstscenarioexpanded

Thus, the amount of contributions that can be made from a cow at location ii and desirable location at location jj is limited by the minimum distance to either end of the order, as after that ll or rr would violate the given constraints for a valid operation.

firstscenarioall

In a 0-indexing system, the contribution from ii and jj can be written as min(i+1,nj)\min(i + 1, n - j).

Now we consider how to calculate contributions created strictly by the second scenario.

Let a cow at position ii already be at a desirable location.

The amount of contributions created is equal to the number of operations that do not involve ii. In the problem, we are given that the number of operations that can occur between 11 and NN can be written as

N(N+1)2.\frac{N (N + 1)}{2}.

Thus, the amount of contributions created from a cow at location ii at a desirable location can be written as the sum of all the possible operations before and after ii:

i(i+1)2+(Ni1)(Ni)2.\frac{i \cdot (i + 1)}{2} + \frac{(N - i - 1) \cdot (N - i)}{2}.

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 (i,j)(i, j).

If we naively checked each pair, this would take O(N)\mathcal{O}(N) 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 ddesd_{des} and a cow's minimum distance to an end as dcowd_{cow}, where ii denotes the cow's location and jj denotes the desired location.

dcow=min(i+1,Ni)d_{cow} = \min(i + 1, N - i)
ddes=min(j+1,Nj)d_{des} = \min(j + 1, N - j)

For a given cow, consider all desirable locations that it can pair with. Each pair contributes min(dcow,ddes)\min(d_{cow}, d_{des}) 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 ddesd_{des} because ddes<dcowd_{des} < d_{cow}, and ones where contributions are limited by dcowd_{cow} because dcow<ddesd_{cow} < d_{des}. We can partition them by creating a sorted array of all ddesd_{des}, then binary searching over this array to determine the index where ddesdcowd_{des} \geq d_{cow}. Before this index, contributions will be limited by ddesd_{des}, and after this index, contributions will be limited by dcowd_{cow}.

firstscenariooptimization

However, this still results in O(N)\mathcal{O}(N) time, since we still have to sum up all ddesd_{des} contributions at the end.

We can optimize this by implementing a prefix sum array that sums the indices [0,i][0, i] of the ddesd_{des} array, so we can find the sum of valid ddesd_{des} from the start of a list to any point in O(1)\mathcal{O}(1) time. Then, all contributions limited by dcowd_{cow} can be found by multiplying dcowd_{cow} by the number of pairs limited.

Thus, the formula for the contributions provided by the first scenario can be found as written below, given mm is the point where dcowd_{cow} becomes less than ddesd_{des}:

prefix[m]+dcow(Nm).\text{prefix}[m] + d_{\text{cow}} \cdot (N - m).

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N \log N)

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 bisect
num_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 ordering
edge_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!