链接:https://www.nowcoder.com/acm/contest/142/G
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
The mode of an integer sequence is the value that appears most often. Chiaki has n integers 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.
输入描述:
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 ≤ 105, 0 ≤ m < n) -- the length of the sequence and the number of integers to delete.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) denoting the sequence.
It is guaranteed that the sum of all n does not exceed 106.
输出描述:
For each test case, output an integer denoting the only maximum mode, or -1 if Chiaki cannot achieve it.
示例1
输入
复制
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
输出
复制
-1
3
3
3
4
#include<bits/stdc++.h>
using namespace std;
struct node{
int t, v;
node(int t = 0, int v = 0) : t(t), v(v) {};
bool operator < (const node &a) const{
return t > a.t;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N;
for(cin >> N; N; N--){
int n, m;
cin >> n >> m;
map <int, int> mp;
for(int i = 1, x; i <= n; i++){
cin >> x;
mp[x]++;
}
int ans = -1;
vector <node> v;
for(auto x : mp) v.emplace_back(x.second, x.first);
sort(v.begin(), v.end());
int p = 0;
int sum = 0;
for(int i = 0; i < v.size(); i++){
while(p < v.size() && v[p].t == v[i].t)
sum += v[p].t, p++;
int now = sum - (v[i].t - 1) * p - 1;
if(now <= m) ans = max(ans, v[i].v);
}
cout << ans << endl;
}
return 0;
}