牛客网暑期ACM多校训练营(第四场) G

链接: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

思路;按每个数的个数进行排序。

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int M = 1e6 + 10;
int t;
ll n,m;
ll ans[M],a[M],b[M],sum1[M];
map<ll,int>maps;
struct node
{
    ll x;
    int num;
    bool operator < (const node &k)
    {
        if(num==k.num)
            return x<k.x;
        else
            return num>k.num;
    }
}e[M];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
       maps.clear();
       ll cnt=0,l=0,i;

       scanf("%lld%lld",&n,&m);
       for(i=0;i<=n;i++)
        e[i].x=0,e[i].num=0,sum1[i]=0;
       for(i=1;i<=n;i++)
       {
           scanf("%lld",&a[i]);
           maps[a[i]]++;
           if(maps[a[i]]==1){
            cnt++;
            b[++l]=a[i];
           }
       }
       for(i=1;i<=l;i++)
       {
           e[i].x=b[i];
           e[i].num=maps[b[i]];
       }
       sort(e+1,e+l+1);//按数的个数进行排序
       sum1[0]=0;
       for(i=1;i<=l;i++)
        sum1[i]+=sum1[i-1]+e[i].num;
       ll ans=-1,res;
       ll c=e[1].num;
       for(i=1;i<=l;i++)
       {
           bool f=0;
           while(e[i].num==c&&i<=l)
           {
               f=1;
               i++;
           }
           c=e[i].num;
           res=sum1[i-2]-(i-2)*e[i-1].num+(i-2);
           //cout<<res<<" "<<c<<endl;
           if(res>m)
           {
               break;
           }
           if(ans<=e[i-1].x)
            ans=e[i-1].x;
           if(f)
            i--;
       }
       printf("%lld\n",ans);
    }
    return 0;
}
/*

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
*/

猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/81324687