electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...
Problems/Arithmetic Game/Editorial

Editorial: Arithmetic Game

nnn — size of array aaa

aaa — array of numbers

This is a minimax game: Alice wants to minimize the final score, Bob wants to maximize it. Both play optimally, and Alice moves first.

Recall that the arithmetic mean uses floor division: ⌊S/n⌋\lfloor S / n \rfloor⌊S/n⌋.

Subtask 1: n=3n = 3n=3 (202020 points)

With only 333 elements, Alice removes one and Bob removes another, leaving a single element as the final score. There are only 333 choices for Alice and 222 remaining choices for Bob — we can check all 666 possibilities with conditional statements.

Time complexity: O(1)O(1)O(1)
Space complexity: O(1)O(1)O(1)

Subtask 2: All aia_iai​ are the same (151515 points)

If all values are equal to some constant ccc, then the arithmetic mean is ⌊nc/n⌋=c\lfloor nc / n \rfloor = c⌊nc/n⌋=c before removal and ⌊(n−2)c/(n−2)⌋=c\lfloor (n-2)c / (n-2) \rfloor = c⌊(n−2)c/(n−2)⌋= after removal. The score never changes, so the answer is always .

Time complexity: O(n)O(n)O(n)
Space complexity: O(n)O(n)O(n)

Subtask 3: n≤103n \leq 10^3n≤103 (252525 points)

We brute-force the minimax. For each possible choice aia_iai​ that Alice can remove, we simulate Bob's optimal response: Bob will pick the element aja_jaj​ (j≠ij \neq ij=i) that maximizes the final score . This means Bob will remove the smallest remaining element (since removing a smaller value keeps the sum larger).

For each Alice choice aia_iai​, we compute the best Bob can achieve. Alice will choose the aia_iai​ that minimizes this value. We then compare the result to the initial score.

Time complexity: O(n2)O(n^2)O(n2)
Space complexity: O(n)O(n)O(n)

Subtask 4: Full Solution (404040 points)

Alice wants to reduce the final score as much as possible, so her optimal strategy is to remove the largest element — this reduces the sum maximally. Bob wants to keep the final score as high as possible, so his optimal strategy is to remove the smallest remaining element — this preserves the sum as much as possible.

Algorithm:

  1. Sort the array aaa.
  2. Compute the initial score: initial=⌊S/n⌋\text{initial} = \lfloor S / n \rfloorinitial=⌊S/n⌋ where S=∑aiS = \sum a_iS=∑ai​.
  3. Remove a[ n− (largest, Alice's choice) and (smallest, Bob's choice).

Time complexity: O(n)O(n)O(n)
Space complexity: O(n)O(n)O(n)

Solution Code

← Back to Problem
c
Tie
⌊(S−ai−aj)/(n−2)⌋\lfloor (S - a_i - a_j) / (n - 2) \rfloor⌊(S−ai​−aj​)/(n−2)⌋
1 ]a[\,n-1\,]
a[n−1]
a[ 0 ]a[\,0\,]a[0]
  • Compute the final score: final=⌊(S−a[0]−a[n−1])/(n−2)⌋\text{final} = \lfloor (S - a[0] - a[n-1]) / (n - 2) \rfloorfinal=⌊(S−a[0]−a[n−1])/(n−2)⌋.
  • Compare and output the result.
  • #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int n;
        cin >> n;
    
        vector<long long> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];
    
        sort(a.begin(), a.end());
    
        long long sumAll = 0;
        for (int i = 0; i < n; i++)
            sumAll += a[i];
    
        long long initial = sumAll / n;
        long long final_score = (sumAll - a[0] - a[n - 1]) / (n - 2);
    
        if (final_score < initial)
            cout << "Alice" << endl;
        else if (final_score == initial)
            cout << "Tie" << endl;
        else
            cout << "Bob" << endl;
    
        return 0;
    }