链接:https://www.nowcoder.com/acm/contest/142/G
来源:牛客网
题目描述
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
题意:每次删去m个数找出删除后尽量众数最多并且最大的一个数。
#include<bits/stdc++.h>//n,m<=1e5 ,a[i]<=1e9
using namespace std;
const int N=100005;
typedef struct mode{
int x,num;
bool operator < (const mode & p1) const {
return num<p1.num;
}
}mode;
int cmp(int a,int b){
return a>b;
}
int main()
{
int n,m;
int T;
scanf("%d",&T);
while(T--){
int sum[N]={0};
int a[N]={0};
mode b[N];
map<int,int>mp;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
mp[a[i]]++;
}
int cnt=0;
map<int,int>::iterator it=mp.begin();
for(;it!=mp.end();it++){
b[cnt].x=it->first;
b[cnt].num=it->second;
cnt++;
}
sort(b,b+cnt);
for(int i=cnt-1;i>=0;i--){
sum[i]=sum[i+1]+b[i].num;
}
/*for(int i=0;i<cnt;i++){
printf("sum[%d] : %d\n",i,sum[i]);
}*/
map<int,int>mp2;
//printf("MMMMMMMMMMM:%d\n",m);
int maxz=-1;
for(int i=0;i<cnt;i++){
int t=mp2[b[i].num]*b[i].num;
//printf("mp2[b[i].x] :%d b[i].num: %d t: %d \n",mp2[b[i].num],b[i].num,t);
int tt=t+sum[i+1]-(mp2[b[i].num]+cnt-i-1)*(b[i].num-1);
//printf("SUM[i+1] %d , cnt-i-1: %d\n",sum[i+1],cnt-i-1);
//printf("%d\n",tt);
if(tt<=m){
maxz=max(b[i].x,maxz);
}
mp2[b[i].num]++;
}
printf("%d\n",maxz);
}
return 0;
}