Table of Contents

ExplanationImplementation

Official Analysis (C++, Java, Python)

Explanation

Let aia_{i} be the hay cow ii likes.

If there are adjacent hays such that ai=ai+1a_{i} = a_{i+1}, we can run a focus group on i,i+1,i+2i, i+1, i+2 or i1,i,i+1i-1, i, i+1. This will result in cow ii and i+1i+1 infecting cow i+2i+2 or i1i-1 with the type of hay they like. We can keep running focus groups like this to eventually infect every cow with hay aia_{i}.

Situations where ai=ai+2a_{i} = a_{i+2} will also create adjacent hays of the same type (by running a focus group on i,i+1,i+2i,i+1, i+2) and therefore this also makes aia_{i} part of the answer.

Implementation

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

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
int test_num;
cin >> test_num;
for (int t = 0; t < test_num; t++) {
int n;
cin >> n;
vector<int> hay(n);

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!