A - クラス委員長の選出 / Election of the Class President 解説 by admin
Gemini 3.0 Flash (Thinking)Overview
This is a problem where you need to find the maximum value among the vote counts of \(N\) students, and determine whether there is “exactly one” student with that maximum value. If there is a unique student with the most votes, output their number (1-indexed); otherwise, output \(-1\).
Analysis
To solve this problem, the following two steps are necessary:
- Find what the maximum number of votes (maximum value) is
- Check how many students received that maximum number of votes
For example, if the vote counts are \([3, 5, 2]\), the maximum value is \(5\). Since only student 2 received \(5\) votes, the answer is 2.
On the other hand, if the vote counts are \([4, 4, 1]\), the maximum value is \(4\). Since both student 1 and student 2 received \(4\) votes, the answer is -1.
Since the number of students \(N\) is at most \(2 \times 10^5\), an \(O(N)\) algorithm that checks the list sequentially from one end is more than sufficient to produce a correct answer within the time limit.
Algorithm
Implement the following steps:
- Finding the maximum value:
Scan through the given list of vote counts \(A\) and find the maximum value
max_votes. - Counting the number of students and recording the index:
Scan through the list once more and count the number of students whose vote count equals
max_votes.- At the same time, record that student’s number (index + 1).
- If the count reaches 2 or more, it is confirmed that the top voter is not unique, so you may terminate the search early.
- Conditional branching:
- If the count is exactly 1, output the recorded student’s number.
- If the count is not 1 (i.e., 2 or more), output
-1.
Complexity
- Time complexity: \(O(N)\) Since we only scan through the list a few times, the computation finishes in time proportional to the number of students \(N\).
- Space complexity: \(O(N)\) This is the amount of memory needed to store the vote counts of the \(N\) students in a list.
Implementation Notes
Conversion to 1-indexed: Arrays (lists) in programming languages typically start from 0, but the student numbers in the problem start from 1. Don’t forget to use
index + 1when producing the output.Initialization of the maximum value: When searching for the maximum value, it is convenient to initialize it to a value less than or equal to the minimum possible vote count of \(0\) (such as \(-1\)).
Early termination: Once two students with the maximum votes are found, it is confirmed that the answer is
-1. To run slightly more efficiently on large datasets, it is standard practice to break out of the loop (break) as soon as the count exceeds 1.Source Code
import sys
def solve():
# 標準入力からすべてのデータを読み込む
input_data = sys.stdin.read().split()
if not input_data:
return
# N(生徒数)と A(各生徒の得票数)を取得
n = int(input_data[0])
a = list(map(int, input_data[1:]))
# 最多得票数を求める
max_votes = -1
for x in a:
if x > max_votes:
max_votes = x
# 最多得票の生徒をカウントし、その番号を保持する
count = 0
winner_idx = -1
for i in range(n):
if a[i] == max_votes:
count += 1
winner_idx = i + 1
# 2人以上見つかった時点で -1 確定なので打ち切る
if count > 1:
break
# 最多得票の生徒が1人ならその番号を、そうでなければ -1 を出力
if count == 1:
print(winner_idx)
else:
print(-1)
if __name__ == "__main__":
solve()
This editorial was generated by gemini-3-flash-thinking.
投稿日時:
最終更新: