Skip to content

USACO 2020 December Contest Bronze Division - Daisy Chains#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Sort the seven numbers, the first two are the first two numbers, then it comes down to some casework in order to decide which number is the third number (either the third or the fourth number from the original array).

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;

    vector<int> v(n);
    for(int i = 0; i < n; i++)
        cin >> v[i];

    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        int sum = 0;
        set<int> s;
        for(int j = i; j < n; j++)
        {
            s.insert(v[j]);
            sum += v[j];
            if(sum % (j - i + 1) == 0 && s.find(sum / (j - i + 1)) != s.end())
                ans++;
        }
    }

    cout << ans;
    return 0;
}
n = int(input())
v = list(map(int, input().split()))

ans = 0
for i in range(n):
    total = 0
    s = set()
    for j in range(i, n):
        s.add(v[j])
        total += v[j]
        if total % (j - i + 1) == 0 and (total // (j - i + 1)) in s:
            ans += 1
print(ans)