A - 人気投票の集計 / Tallying the Popularity Vote Editorial by admin Editorial for printing
gemini-3-flash-previewOverview
Given \(N\) votes, the problem asks us to tally the number of votes for each class and count how many classes received more votes than the “number of abstentions (\(A_i=0\)).”
Analysis
To solve this problem, the following two steps are needed: 1. Tallying each vote target: Accurately count how many votes each class (or abstention) received. 2. Checking the condition: Compare each class’s vote count with the number of abstentions.
Efficient Tallying
Looking at the constraints, the number of students \(N\) is at most \(2 \times 10^5\). Therefore, if we repeatedly “scan all data to count votes” for each class, it would take \(O(N^2)\) time in the worst case, which would not meet the time limit (TLE).
Instead, by using a hash map (dictionary or Counter in Python) or an array indexed by class number, we can tally all votes by scanning the data only once. This allows us to complete the tallying in \(O(N)\).
Key Point for the Condition Check
The condition for a “championship candidate” is “vote count for class \(x\) \(>\) number of abstentions.” First, we identify the number of abstentions (\(A_i=0\)), and using that value as the threshold, we compare it against the vote count of each other class.
Algorithm
- Tallying: Scan the input array \(A\) and count the occurrences of each element.
- Using Python’s
collections.Counter, we can easily obtain a dictionary of “value: occurrence count.”
- Using Python’s
- Getting the threshold: From the counted results, retrieve the occurrence count of
0. If0never appears, treat it as \(0\) people. - Comparison and counting: For each class other than
0in the tallied results, perform the following:- If that class’s vote count is greater than the number of abstentions found in step 2, increment the answer variable (
ans) by \(1\).
- If that class’s vote count is greater than the number of abstentions found in step 2, increment the answer variable (
- Output: Print the final value of
ans.
Complexity
- Time complexity: \(O(N)\)
- Reading the input takes \(O(N)\), tallying with
Countertakes \(O(N)\), and scanning the tallied results (at most \(N\) distinct values) takes \(O(N)\), so the overall complexity is \(O(N)\).
- Reading the input takes \(O(N)\), tallying with
- Space complexity: \(O(N)\)
- This is because a hash map (or array) of at most \(N\) elements is needed to store the vote count for each class.
Implementation Notes
When there are no abstentions: By implementing it as
counts.get(0, 0), even if the input does not contain0, no error occurs and the number of abstentions is correctly treated as \(0\).Excluding class 0: When counting championship candidates, note that class
0(abstention) itself must be excluded from the comparison targets.Source Code
import sys
from collections import Counter
def main():
# 入力を標準入力からすべて読み込む
input_data = sys.stdin.read().split()
if not input_data:
return
# N の読み込み
n = int(input_data[0])
# A_i の読み込み
a = list(map(int, input_data[1:]))
# 各投票先の出現回数をカウント
counts = Counter(a)
# 棄権(A_i = 0)の人数を取得
abstentions = counts.get(0, 0)
# 優勝候補の数をカウント
ans = 0
for class_id, count in counts.items():
# クラス 0 は棄権なのでスキップ
if class_id == 0:
continue
# 棄権した人数より多い票を得ているクラスをカウント
if count > abstentions:
ans += 1
# 結果を出力
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gemini-3-flash-preview.
posted:
last update: