Official

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

(Not recommended) Qwen3-Coder-480B

Overview

Given the results of each student voting for a class, find the number of classes that received more votes than the number of abstainers.

Analysis

In this problem, we need to count how many votes each class received and determine whether that count is greater than the number of abstainers.

A naive approach would be to count the number of people who voted for each class every time, but in the worst case where \(N\) people vote for \(N\) different classes, counting each time results in \(O(N^2)\) time complexity. This is too slow for the constraint \(N \leq 2 \times 10^5\) (TLE).

Therefore, we need to count the votes for each class efficiently in advance. This can be achieved using Counter, which efficiently counts the occurrences of elements. By using Counter, we only need to scan all votes once to obtain the vote count for each class, resulting in \(O(N)\) time complexity.

Also, abstainers are represented by \(A_i = 0\), so we include them in the count and use the result for comparison later.

Algorithm

  1. Read the input.
  2. Use collections.Counter to count the votes for each class (and abstentions).
  3. Retrieve the number of abstainers (the value for key 0).
  4. For each class whose key is not 0, check whether its vote count is greater than the number of abstainers, and count the number of classes satisfying this condition.
  5. Output the number of classes satisfying the condition.

Example

For the following input:

5
1 2 0 1 0
  • Votes:
    • Student 1 → Class 1
    • Student 2 → Class 2
    • Student 3 → Abstain
    • Student 4 → Class 1
    • Student 5 → Abstain
  • Vote counts:
    • Class 1: 2 votes
    • Class 2: 1 vote
    • Abstentions: 2 people
  • Since there are 2 abstainers, Class 1 (2 votes) is a winner candidate. Class 2 (1 vote) does not qualify.
  • Therefore, the answer is 1.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Implementation Notes

  • By using collections.Counter, we can efficiently count the occurrences of each element.

  • Key 0 represents abstention, so it should be handled separately from other classes.

  • The vote count for each class can be obtained with counter[class_id], and for keys that do not exist, 0 is returned by default, so it is safe.

    Source Code

from collections import Counter

N = int(input())
A = list(map(int, input().split()))

# カウント辞書を作成
counter = Counter(A)

# 棄権者数(A_i == 0 の人数)
abstain_count = counter[0]

# 優勝候補の数
candidate_count = 0

# 各クラスについてチェック(キーが0でないもの)
for class_id, vote_count in counter.items():
    if class_id != 0 and vote_count > abstain_count:
        candidate_count += 1

print(candidate_count)

This editorial was generated by qwen3-coder-480b.

posted:
last update: