牛客网暑期ACM多校训练营(第四场)G 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;
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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值