Official

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

Claude 4.5 Opus

Overview

This problem asks you to count the votes for each class and determine how many classes received more votes than the number of abstentions.

Analysis

Problem Breakdown

First, let’s organize what the problem is asking:

  • \(N\) students each vote for one class (or abstain)
  • \(A_i = 0\) means abstention
  • A “winning candidate” is a class whose vote count is strictly greater than the number of abstentions
  • Find the number of winning candidates

Working Through an Example

For example, with \(N = 7\) and \(A = [1, 2, 1, 0, 1, 0, 2]\): - Votes for class 1: 3 votes - Votes for class 2: 2 votes - Number of abstentions (\(A_i = 0\)): 2 people

Checking the winning candidate condition “votes > abstentions”: - Class 1: \(3 > 2\) → Winning candidate ✓ - Class 2: \(2 > 2\) → Not a winning candidate (ties don’t count)

Therefore, the answer is 1.

Naive Approach

If we scan through the array for each class to count votes, we would scan \(N\) times for up to \(N\) different classes, resulting in \(O(N^2)\) complexity. With \(N = 2 \times 10^5\), this risks TLE (Time Limit Exceeded).

Efficient Approach

By scanning the array only once and counting votes for each class all at once, we can then reference each class’s vote count in constant time. We use a hash map (dictionary) for this.

Algorithm

  1. Count votes: Use Counter to tally votes for each class (including 0)
  2. Get abstention count: The “vote count” for class 0 is the number of abstentions
  3. Determine winning candidates: For each class other than 0, check if its vote count exceeds the abstention count
  4. Output result: Output the number of classes satisfying the condition

Complexity

  • Time Complexity: \(O(N)\)
    • \(O(N)\) for counting votes
    • \(O(N)\) for iterating through classes (at most \(N\) different classes)
  • Space Complexity: \(O(N)\)
    • Data stored in Counter (at most \(N\) different classes)

Implementation Notes

  1. Using Counter: Python’s collections.Counter makes it easy to count occurrences of each element in a list.

  2. Getting abstention count: Using votes.get(0, 0) ensures we return 0 when there are no abstentions (when the key doesn’t exist).

  3. Excluding class 0: Since abstention (\(A_i = 0\)) is not a class, it must be excluded from winning candidate consideration. We explicitly skip it in the loop with if class_id == 0: continue.

  4. Strict inequality: The condition is “more than the number of abstentions,” so be careful to use > instead of >=.

Source Code

from collections import Counter

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

# Count votes for each class
votes = Counter(A)

# Number of abstentions (count of A_i = 0)
abstention = votes.get(0, 0)

# Count the number of winning candidate classes
count = 0
for class_id, vote_count in votes.items():
    # Class 0 (abstention) is not a candidate
    if class_id == 0:
        continue
    # If vote count exceeds abstention count, it's a winning candidate
    if vote_count > abstention:
        count += 1

print(count)

This editorial was generated by claude4.5opus.

posted:
last update: