牛客网暑期ACM多校训练营(第四场) Maximum Mode(思维)

链接: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;
#define LL long long
#define clr(a) memset(a,0,sizeof(a))

const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int N = 2010;

int n,m,t;
int a[MAXN],cnt[MAXN],sum[MAXN];
map<int,int>mp;
int main(){
    scanf("%d",&t);
    while(t--){
        mp.clear();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            mp[a[i]]++;
            sum[i] = 0;
            cnt[i] = 0;//记录众数出现的个数
        }
        map<int,int>::iterator it;
        for(it=mp.begin();it!=mp.end();it++){
            cnt[(*it).second] ++;
        }
        for(int i=n-1;i;i--){
            cnt[i] += cnt[i+1];
            sum[i] = sum[i+1]+cnt[i];
        }
        int maxx = -1;
        for(it=mp.begin();it!=mp.end();it++){
            if(sum[(*it).second]-1 <= m)
                maxx = max(maxx,(*it).first);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81320217