LOJ#6297. 「CodePlus 2018 3 月赛」投票统计
Description:
为了总结过去一段时间的命题工作,王队长组织了“我最喜欢的题目”评选活动,并邀请各位选手给题目进行投票。
具体来说,每道题目有一个正整数作为它的编号,一共有n名选手给它们进行投票,每位选手投且仅投给一道题,其中第i位选手所投票的题目编号为 ai 。
由于投票的选手众多,所以王队长请你来帮忙统计得票数。你需要找出收获选手投票最多的题目数量与他们的编号,并按从小到大的顺序列出这些编号。但这里有一个例外情况:如果所有被投票的题目得票数都相同,则王队长认为这次活动比较失败,你应该输出-1。
Input:
输入数据第一行包含一个正整数T,表示测试数据的组数,各组数据之间没有空行。
接下来2T行,依次描述每组数据:
每组数据包含两行,其中第一行包含一个正整数n,表示参与这次活动的选手人数。第二行包含n个由空格隔开的正整数 a1,…,an,其中第i个数 ai 表示第i位选手所投票的题目编号。
Output:
输出应由T组数据组成,各组数据之间没有空行。对于每一组数据:
若没有出现题面所述的例外情况,则这组测试数据输出两行,其中第一行输出一个正整数m,表示收获选手投票最多的题目数量,第二行按从小到大的顺序输出m个正整数,表示这些题目的编号。
若出现题面所述的例外情况,则这组测试数据输出一行,请输出-1。
Sample Input:
3
10
2 6 1 2 1 1 2 6 7 1
10
10 3 6 6 3 10 6 6 6 2
10
8 8 10 10 10 10 8 5 8 8
Sample Output:
1
1
1
6
1
8
Sample Input:
3
10
1 4 3 1 8 8 7 2 8 7
10
1 10 9 1 3 2 9 9 2 1
10
4 1 5 4 1 9 5 5 4 1
Sample Output:
1
8
2
1 9
3
1 4 5
Sample Input:
3
10
3 3 10 8 8 3 10 8 10 3
10
2 2 8 6 8 4 2 4 4 8
10
6 2 5 6 7 5 7 10 2 10
Sample Output:
1
3
3
2 4 8
-1
题目连接
这道题目用map很方便,第一个数存编号,第二个数存票数,然后再进行统计判断即可。
一定要注意输出格式,最后一个数据之后不能有多余空格,这点很坑。
AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const double eps = 1e-5;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
int t;
int n;
map<int, int> cor;
int main() {
//fropen("in.txt", "r", stdin);
cin >> t;
while (t--) {
cor.clear();
cin >> n;
int ans = 0;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
cor[num]++;
if (cor[num] > ans) {
ans = cor[num];
}
}
int cnt = 0;
for (auto it = cor.begin(); it != cor.end(); ++it) {
if (ans == (it -> second)) {
cnt++;
}
}
if (cor.size() == cnt) {
cout << -1 << endl;
}
else {
cout << cnt << endl;
bool flag = 1;
for (auto it = cor.begin(); it != cor.end(); ++it) {
if (ans == (it -> second)) {
if (flag) {
cout << it -> first;
flag = 0;
}
else {
cout << " " << it -> first;
}
}
}
cout << endl;
}
}
return 0;
}