Official

A - 人気投票の集計 / Tallying the Popularity Vote Editorial by admin Editorial for printing

GPT 5.2 High

Overview

This is a problem where you count the number of abstentions (\(A_i=0\)) and then count how many classes have more votes than that number.

Analysis

The condition for being a winning candidate is “votes for class \(x\) \(>\) number of abstentions.” In other words, first determine the number of people who abstained as \(cnt0\), and then simply compare each class’s vote count with \(cnt0\) to make the judgment.

If you naively “scan the entire array for each class to count votes,” the number of classes is at most \(N\) and each scan looks at up to \(N\) elements, resulting in \(O(N^2)\) time complexity. For \(N \le 2\times 10^5\), this will definitely not finish in time (TLE).

Instead, in a single pass, we simultaneously tally: - The number of abstentions \(cnt0\) - An array (or dictionary) storing the vote count for each class

Then, at the end, we count how many classes satisfy the condition.

Example: For \(A=[0,2,2,1,0]\), the number of abstentions is \(cnt0=2\).
Class 1 has 1 vote and \(1 \nleq 2\), class 2 has 2 votes and \(2 \nleq 2\), so the number of winning candidates is 0 classes.

Algorithm

  1. Read the input.
  2. Initialize \(cnt0=0\) and the vote count array \(freq[0..N]\) to \(0\).
  3. For each vote target \(a\):
    • If \(a=0\), increment \(cnt0\) by 1 (count abstentions)
    • Otherwise, increment \(freq[a]\) by 1 (count votes for class \(a\))
  4. For \(x=1\) to \(N\), count and output the number of values satisfying \(freq[x] > cnt0\).

Complexity

  • Time complexity: \(O(N)\) (one pass for tallying + one pass for checking)
  • Space complexity: \(O(N)\) (vote count array \(freq\))

Implementation Notes

  • Since \(A_i\) is at most \(N\), you can directly count using an array of length \(N+1\) (where the index corresponds to the class number).

  • Since abstentions are not a class, instead of putting them in \(freq[0]\), counting them with a separate variable \(cnt0\) makes the condition check clearer.

  • Since the input can be large, in Python it is safer to use fast input methods like sys.stdin.buffer.read().

    Source Code

import sys

def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    if not data:
        return
    N = data[0]
    A = data[1:1+N]

    cnt0 = 0
    freq = [0] * (N + 1)
    for a in A:
        if a == 0:
            cnt0 += 1
        else:
            freq[a] += 1

    ans = sum(1 for x in range(1, N + 1) if freq[x] > cnt0)
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

posted:
last update: