链接:https://www.nowcoder.com/acm/contest/142/G
来源:牛客网

Maximum Mode

时间限制: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

题意:mode 数是出现次数最多的数。

输入n个数,删除m个数后mode数只能有一个,并且要尽量的大。

方法:记录每个数出现的次数,枚举每个数作为mode数时至少需要删除其他多少数。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
map<ll,ll>mp;
struct node
{int val,num,sum;
};
node b[maxn];
int a[maxn];
bool cmp(node a,node b)
{return a.num<b.num;
}
int main()
{int t,n,m,i,cnt,k,sum2,sum1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%d",&a[i]);sort(a+1,a+1+n);cnt=0;k=0;sum2=0;for(i=1;i<n;i++){if(a[i]==a[i+1]){cnt++;}else{b[++k].val=a[i];b[k].num=cnt+1;b[k].sum=-1;sum2+=b[k].num;cnt=0;}}if(a[n-1]==a[n]){b[++k].val=a[n];b[k].num=cnt+1;b[k].sum=-1;sum2+=b[k].num;}else{b[++k].val=a[n];b[k].num=1;b[k].sum=-1;sum2+=1;}sort(b+1,b+1+k,cmp);ll temp;cnt=2;//cout<<sum2<<endl;b[1].sum=sum2-b[1].num-(b[1].num-1)*(k-1);//cout<<b[1].sum<<"!!"<<endl;sum1=b[1].num;for(i=2;i<=k;i++){if(b[i].num==b[i-1].num){b[i].sum=b[i-1].sum;cnt++;sum1+=b[i].num;}else{temp=sum2-sum1-b[i].num-(b[i].num-1)*(k-cnt);b[i].sum=temp;cnt++;sum1+=b[i].num;}}int maxn=-1;int flag=0;for(i=1;i<=k;i++){if(m>=b[i].sum){flag=1;maxn=max(maxn,b[i].val);}}if(flag==0)printf("-1\n");elseprintf("%d\n",maxn);}
}
/*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
18 15
2 2 9 9 1 1 3 3 3 4 4 4 5 5 5 5 5 5*/

Maximum Mode相关推荐

  1. 【C++】C++11 STL算法(六):最小/最大操作(Minimum/maximum operations)、比较运算(Comparison operations)

    目录 最小/最大操作(Minimum/maximum operations) 一.max 1.原型: 2.说明: 3.官方demo 二.max_element 1.原型: 2.说明: 3.官方demo ...

  2. Lintcode42 Maximum Subarray II solution 题解

    [题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...

  3. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  4. UVA11059 Maximum Product

    问题链接:UVA11059 Maximum Product.基础级练习题,用C语言编写程序. 题意简述:输入n个整数序列,有正有负,求这个序列中最大连续累乘的子序列,其最大的值为多少.如果结果为负数, ...

  5. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  6. 贪心 ---- E. Maximum Subsequence Value[位运算]

    E. Maximum Subsequence Value 题目大意:有点难解释..建议自己看题.我这里就粗略解释:给定一个数组aaa,要求选出具有最大价值的子序列.假设此子序列的长度为kkk,那么最大 ...

  7. Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...

  8. Codeforces 454C - Little Pony and Expected Maximum

    454C - Little Pony and Expected Maximum 思路: m面的骰子掷n次,总共有m^n种情况,如果一种情况的最大值是m,那么它肯定包含m,那我们在所有情况下挖掉不包含m ...

  9. pandas使用max函数和min函数计算dataframe日期(时间)数据列中最大日期和最小日期对应的数据行(maximum and minimum date or time row)

    pandas使用max函数和min函数计算dataframe日期(时间)数据列中最大日期和最小日期对应的数据行(maximum and minimum date or time row in data ...

  10. pandas计算滑动窗口中的最大值实战(Rolling Maximum in a Pandas Column):计算单数据列滑动窗口中的最大值、计算多数据列滑动窗口中的最大值

    pandas计算滑动窗口中的最大值实战(Rolling Maximum in a Pandas Column):计算单数据列滑动窗口中的最大值.计算多数据列滑动窗口中的最大值 目录

最新文章

  1. python语言程序设计基础网课-Python语言程序设计基础答案
  2. python读取文件with open_python 文件读写操作open和with的用法
  3. Mr.J-- jQuery学习笔记(二十)--节点操作方法
  4. 创建的函数带有编译错误。_AST实现函数错误的自动上报(原理到实践)
  5. 常用类 (六) ----- String类与字符串
  6. lnmp安装完之后的一些注意事项
  7. 视频剪辑软件对比之:会声会影与剪映
  8. python第二课知识点总结
  9. vs2017安装和使用教程(详细)
  10. 基于格密码的算法研究
  11. 读书笔记:深度学习入门-基于python的理论与实现(俗称鱼书)
  12. legion--一款开源,易用,扩展性强的半自动化渗透测试工具
  13. Photoshop教程:10秒闪电搞定照片构图
  14. 简单谈谈MySQL的两阶段提交
  15. 淘宝内乱持续 QQ盛大京东“趁火打劫”
  16. 用Navicat连接阿里云数据库RDS
  17. 力扣每日一题(九——保持城市天际线)
  18. c++超级简单的计算器
  19. 1068 万绿丛中一点红(20 分)----(一点红,全是绿.)
  20. 新零售未来的发展趋势怎么样?

热门文章

  1. C++基础——非类型模板参数
  2. 电脑win7语音怎么测试软件,win7话筒怎么测试 win7话筒测试方法【图文】
  3. 计算机应用技术教程的答案,大学计算机应用技术教程答案
  4. python基本代码教程-Python入门教程丨1300多行代码,让你轻松掌握基础知识点
  5. python中文叫什么-python中文别名
  6. python画柱状图-Python绘制柱状图
  7. 学python可以做什么职业-业余学Python能做什么?对职业发展有什么帮助?
  8. 专科python应届生工资多少-应届生自学Python两个月,为什么找不到工作?
  9. iOS中 语音识别功能/语音转文字教程具体解释 韩俊强的博客
  10. 什么是pdi检测_汽车pdi检测是什么?如何知道新车做没做pdi