Maximum Mode

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

题意:

在n个数中删掉m个数,使现在的众数保证在剩余数中最大的情况下,求出最大值

分析:

贪心考虑剩余m个数的情况

代码:

#include<bits/stdc++.h>
using namespace std;
int a[100005],cnt;
struct node
{
    int num,val;
    bool operator<(const node &aa)const
    {
        if(aa.num!=num)
            return num>aa.num;
        return val>aa.val;
    }
}v[100005];
int main()
{
    int t,i,j,k,l,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        m=n-m;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        cnt=0;
        k=1;
        for(i=1;i<n;i++)
        {
            if(a[i]==a[i-1])
                k++;
            else
            {
                v[cnt].num=k;
                v[cnt].val=a[i-1];
                k=1;
                cnt++;
            }
        }
        v[cnt].num=k;
        v[cnt].val=a[i-1];
        cnt++;
        sort(v,v+cnt);
        int maxn,left;
        left=n;
        maxn=-1;
        //printf("ssss   %d\n",cnt);
        for(i=0;i<cnt;i=j)
        {
            //printf("%d %d\n",v[i].num,v[i].val);
            for(j=i;j<cnt&&v[i].num==v[j].num;j++)
            {
                left-=v[j].num;
            }
            for(k=i;k<j;k++)
            {
                if(v[k].num>=m)
                    maxn=max(maxn,v[k].val);
                else
                if(m<=left+(j-1)*(v[k].num-1)+v[k].num)//大于当前数的在前面出现一定会被选中,因为数量比当前大。(不会影响结果不会改变maxn的值)比当前数小的也不会影响结果会记录在j-1中
                maxn=max(maxn,v[k].val);
            }
        }
        printf("%d\n",maxn);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值