链接: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 ≤ 10^5, 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 ≤ 10^9)denoting the sequence.It is guaranteed that the sum of all n does not exceed 10^6

输出描述:

For each test case, output an integer denoting the only maximum mode, or -1 if Chiaki cannot achieve it.

输入例子:
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

-->

示例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;
const int MAXN=1e5+;
int a[MAXN];
map<int, int>ma;
map<int ,int>mb;
int sum[MAXN],cnt[MAXN];
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
ma.clear();
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
ma[a[i]]++;
sum[i]=;
cnt[i]=;
}
for(auto &i:ma) cnt[i.second]++;
for (int i = n-; i ; i--) { //求出后缀和
cnt[i]+=cnt[i+];
sum[i]=sum[i+]+cnt[i];//从后向前进行,如果一个数出现n次,在向前加的过程中就会加n次。保证后缀和正确
}
int MAX=-;
for(auto &i:ma){
if(sum[i.second]-<=m) MAX=max(MAX,i.first);//枚举答案,-1是因为只要保证当前这个众数比其他的多一即可
}
printf("%d\n",MAX);
}
return ;
}
 

牛客第四次多校Maximum Mode的更多相关文章

  1. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  2. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

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

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...

  8. 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...

  9. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

随机推荐

  1. Consider everything deeply but still remain fearless.

    Consider everything deeply but still remain fearless.愿你能深思熟虑,但始终无所畏惧.

  2. 【干货】JavaScript DOM编程艺术学习笔记1-3

    从7月29号到8月8号,断断续续地看完了这本书,做了部分实践联系.总体感觉本书真的只能算是个入门,学完之后看到库的那一章才感觉是个大坑,实践中大部分应该都是用的现成的库吧,所以还要重新学习一个库,但是 ...

  3. slfj4 + logback

    slf4j:(Simple Logging Facade for Java,简单日志门面),它不是具体的日志解决方案,只服务于各种各样的日志系统.在使用SLF4J的时候,不需要再代码中或配置文件中指定 ...

  4. WPF样式学习三

    SnapsToDevicePixels 获取或设置在呈现过程,该值来确定呈现此元素是否应使用特定于设备的像素设置. 这是一个依赖项属性. true ,如果元素应以符合呈现到设备像素;否则, false ...

  5. happy2018暑期集训课后习题001

    根据需求补全下面的代码: 需求: 每行输入三个整数a.b.c,根据a的值不同对b和c进行不同的操作: a为0时,计算b+c a为1时,计算b-c a为2时,计算b*c a为3时,计算b/c 并输出操作 ...

  6. 判断一个字符串是否为GUID的方法

    在.net4.0后出现的TryParse方法可以完成这件事情,至于4.0之前的话,也只能是guid.parse()这个方法加上异常去判断了. 方法具体如下: Guid newGuid = Guid.E ...

  7. POJ-2135 Farm Tour---最小费用最大流模板题(构图)

    题目链接: https://vjudge.net/problem/POJ-2135 题目大意: 主人公要从1号走到第N号点,再重N号点走回1号点,同时每条路只能走一次. 这是一个无向图.输入数据第一行 ...

  8. Hybrid App开发之css样式使用

    前言: 前面学习了html,今天学习一下css的基本使用,看下html与css之间是如何结合来编写前端网页的. CSS 是什么? CSS 是 Cascading Style Sheets(级联样式表) ...

  9. 2018. 2.4 Java中集合嵌套集合的练习

    创建学生类有姓名学校和年龄 覆盖toString() 1.创建三个学生对象,放到集合ArrayList 2.输出第2名学生的信息 3.删除第1个学生对象 4.在第2个位置插入1个新学生信息 5.判断刘 ...

  10. Python实现注册和登录

    一.注册账号需要实现的功能 1.输入:用户名,密码,密码确认 2.限制1:输入的账号和密码不能为空 3.限制2:两次输入密码必须一致 4.限制3:用户名不能重复 5.限制4:错误次数为4次 6.用字典 ...