P4263 [Code+#3] Vote Counting
Description
To review the problem-setting work over the past period, Captain Wang organized a "My Favorite Problem" selection and invited all contestants to vote for the problems.
Specifically, each problem has a positive integer as its ID. There are $n$ contestants voting; each contestant votes for exactly one problem, and the $i$-th contestant votes for the problem with ID $a_i$.
Since there are many voters, Captain Wang asks you to help tally the votes. You need to find the number of problems that received the most votes and their IDs, and list these IDs in ascending order. However, there is one exception: if all problems that received at least one vote have the same number of votes, then Captain Wang considers the event unsuccessful, and you should output -1.
Input Format
The first line contains a positive integer $T$, the number of testdata groups. There is no blank line between groups.
The next $2T$ lines describe each group in order:
Each group contains two lines. The first line contains a positive integer $n$, the number of contestants participating. The second line contains $n$ space-separated positive integers $a_1\sim a_n$, where the $i$-th number $a_i$ is the ID of the problem voted for by the $i$-th contestant.
Output Format
The output consists of $T$ groups, with no blank lines between groups. For each group:
If the exception described in the statement does not occur, output two lines for this group. On the first line, output a positive integer $m$, the number of problems that received the most votes. On the second line, output $m$ positive integers in ascending order, which are the IDs of these problems.
If the exception occurs, output one line for this group: output -1.
Explanation/Hint
Test point ID|Range of $n$|Range of $a_i$|Special notes
-|-|-|-
1|$=2000$|$\leq n$|The problem with the most votes is unique
2|Same as above|Same as above|Same as above
3|Same as above|$\leq 10^9$|Same as above
4|Same as above|$\leq n$|The exception will not occur
5|Same as above|Same as above|Same as above
6|Same as above|$\leq 10^9$|Same as above
7|Same as above|$\leq n$|None
8|Same as above|Same as above|Same as above
9|Same as above|$\leq 10^9$|Same as above
10|Same as above|Same as above|Same as above
11|$=100000$|$\leq n$|The problem with the most votes is unique
12|Same as above|Same as above|Same as above
13|Same as above|$\leq 10^9$|Same as above
14|Same as above|$\leq n$|The exception will not occur
15|Same as above|Same as above|Same as above
16|Same as above|$\leq 10^9$|Same as above
17|Same as above|$\leq n$|None
18|Same as above|Same as above|Same as above
19|Same as above|$\leq 10^9$|Same as above
20|Same as above|Same as above|Same as above
Credit: https://www.luogu.org/discuss/show?postid=35727.
Translated by ChatGPT 5