[USACO25JAN] It's Mooin' Time II B Solution

· · 题解

Algorithms

The Problem

Given the sequence S of length n ,how many distinct triplets (t_1, t_2, t_3) exist in S such that t_1 \ne t_2 = t_3, and (t_1, t_2, t_3) forms a (not necessarily contiguous) subsequence of S?\ Two triplets are considered different if any one of their elements differs.

Analysis

Since t_2 = t_3, only the numbers that appear at least twice in the sequence can be candidates for t_2 and t_3 in a valid triplet. We can identify such numbers by counting the frequency of each number in S, and collect all numbers with frequency \ge 2 into a set A.

Now, consider a greedy approach: For any a \in A, we can fix the last two occurrences of a in S as t_2 and t_3. Then, any element before the second-to-last occurrence of a can be used as t_1 (as long as it is different from a). Since we are forming a subsequence (not necessarily contiguous), this greedy choice ensures we account for all valid triplets involving a. Therefore, this approach is correct.

Let i be the index of the second-to-last occurrence of a in S, and j be the index of the last occurrence. To compute the number of valid choices for t_1, we need to know how many distinct elements occur in the range [1, i - 1]. This will give the number of valid t_1 satisfying t_1 \ne t_2 = t_3 = a.

To achieve this efficiently, we use a prefix sum array prefixSum, where for each position pos, prefixSum[pos] records the number of unique elements seen in S[1 \ldots pos]. When we process the sequence from the beginning, for each number's first occurrence, we increment the corresponding prefixSum[pos] by 1. Then we can compute prefix sums cumulatively.

The contribution of each a \in A (labeled d_a) to the final answer is:

d_a = prefixSum[i] - 1

The part -1 is designed to prevent that the occurrence of a makes contribution to the result by mistake. For the instance below, if we do not minus 1, the value of d_5 will be wrong:

S = \{1,2,5,3,4,5,5\} prefixSum[5] = 5 \,\, {\color{red}\neq} \,\, d_5 = 4

Eventually, for the answer (labeled ans),we get:

ans = \sum_{a \in A} d_a

Just output the ans with print statement.

Time\,Complexity\,O(n)

Code

//C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int total;
    cin >> total;
    //Input the length of the sequence
    vector<int> count (total, 0);
    //Record the occurrence
    vector<int> posiA (total, -1);
    vector<int> posiB (total, -1);
    //Record the indices of last two occurrence of a specific number
    vector<int> a;
    int prefixSum[1000020];
    memset(prefixSum, 0, sizeof(prefixSum));
    //Prefix Sum
    for (int i = 0; i < total; i++) {
        int input;
        cin >> input;
        count[input]++;
        if (count[input] == 1) {
            posiA[input] = i;
            prefixSum[i] = 1;
        } else if (count[input] == 2) {
            posiB[input] = i;
            a.push_back(input);
        } else if (count[input] >= 3) {
            posiA[input] = posiB[input];
            posiB[input] = i;
        }
    }
    for (int i = 1; i < total; i++) {
        prefixSum[i] = prefixSum[i - 1] + prefixSum[i];
    }
    //Calculate for the Prefix Sum array
    int ans(0);
    for (int i : a) {
        ans += prefixSum[posiA[i]] - 1;
    }
    //Solve for answer
    cout << ans;
    //Output
    return 0;
}