Description:

The mode of an integer sequence is the value that appears most often. Chiaki has n integers a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an. She woud like to delete exactly m of them such that: the rest integers have only one mode and the mode is maximum.

Input:

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m ( 1 n 1 0 5 , 0 m &lt; n 1 ≤ n ≤ 10^5, 0 ≤ m &lt; n 1n105,0m<n) – the length of the sequence and the number of integers to delete.
The second line contains n integers a 1 , a 2 , . . . , a n a_1, a_2, ..., a_n a1,a2,...,an ( 1 a i 1 0 9 1 ≤ a_i ≤ 10^9 1ai109) denoting the sequence.
It is guaranteed that the sum of all n does not exceed 1 0 6 10^6 106.

Output:

For each test case, output an integer denoting the only maximum mode, or -1 if Chiaki cannot achieve it.

Sample Input:

5
5 0
2 2 3 3 4
5 1
2 2 3 3 4
5 2
2 2 3 3 4
5 3
2 2 3 3 4
5 4
2 2 3 3 4

Sample Output:

-1
3
3
3
4

题目链接

枚举每种数字个数中的最大值为答案,判断是否可以删除其它数使其成为众数,取最大值。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 4e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 0;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int t;
	read(t);
	for (int Case = 1, n, m; Case <= t; ++Case) {
		read(n); read(m);
		map<int, int> mp;
		for (int i = 0, x; i < n; ++i) {
			read(x);
			mp[x]++;
		}
		vector<PII> vec(mp.begin(), mp.end());
		sort(vec.begin(), vec.end(), [&] (const PII &a, const PII &b) {
			if (a.second == b.second) {
				return a.first > b.first;
			}
			return a.second > b.second;
		});
		int ans = -1;
		int amount = 0;
		for (int i = 0; i < int(vec.size()); ++i) {
			if (vec[i].second != vec[i - 1].second) {
				int cnt = i;
				for (int j = i + 1; vec[j].second == vec[j - 1].second && j < int(vec.size()); ++j) {
					amount += vec[j].second;
					cnt++;
				}
				int temp = amount - cnt * (vec[i].second - 1);
				if (temp <= m && vec[i].first > ans) {
					ans = vec[i].first;
				}
				amount += vec[i].second;
			}
		}
		printf("%d\n", ans);
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}