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

1

, a

2

, ..., a

n

 (1 ≤ a

i

 ≤ 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 思维题
考虑删去m个数后剩余的数和总的不同数之间的关系
如果剩下的数小于等于总的不同数,暴力枚举出现次数大于2的数取最大值
如果只剩一个数,暴力枚举最大的数
否则枚举每个大于等于2的数,看加上这个数出现的次数和加上大于等于他的数每个减一(保证出现次数低于这个数)再加上小于他的数的和最后结果是否大于剩下的数,取大于的数里面的最大数就行
中间有些细节需优化看代码把
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5;
const ll mod = 1e12 + 7;
struct node {
ll x, y;
};
map<ll,ll> mm;
node a[maxn+10];
bool cmp( node p, node q ) {
if( p.y == q.y ) {
return p.x > q.x;
}
return p.y > q.y;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll T;
cin >> T;
while( T -- ) {
for( ll i = 0; i < maxn; i ++ ) {
a[i].x = a[i].y = 0;
}
mm.clear();
ll n, m, j = 0;
cin >> n >> m;
ll all = n;
for( ll i = 0, t; i < n; i ++ ) {
cin >> t;
mm[t] ++;
}
for( map<ll,ll>::iterator it = mm.begin(); it != mm.end(); it ++ ) {
a[j].x = (*it).first, a[j].y = (*it).second;
j ++;
}
sort( a, a+j, cmp );
ll num = n - m, ans = -1;
if( num == 1 ) {
ans = 0;
for( ll i = 0; i < j; i ++ ) {
ans = max( ans, a[i].x );
}
} else if( num <= j ) {
ans = 0;
for( ll i = 0; i < j; i ++ ) {
if( a[i].y >= 2 ) {
ans = max( ans, a[i].x );
} else {
break;
}
}
} else {
ll t = num - j + 1;
ans = 0;
for( ll i = 0; i < j; i ++ ) {
if( a[i].y >= t ) {
ans = max( ans, a[0].x );
} else {
break;
}
}
map<ll,ll> mp;
for( ll i = 0; i < j; i ++ ) { //记录出现次数一样的数的个数方便后面加减
mp[a[i].y] ++;
}
for( ll i = 0; i < j; i ++ ) {
if( a[i].y >= 2 ) {
if( i > 0 && a[i].y == a[i-1].y ) {
} else {
ll sum = a[i].y + i*(a[i].y-1);
if( sum >= num ) {
ans = max( ans, a[i].x );
} else {
ll allnum = all - a[i].y*mp[a[i].y];
sum += (a[i].y-1)*(mp[a[i].y]-1);
if( allnum + sum >= num ) {
ans = max( ans, a[i].x );
}
}
}
}
all -= a[i].y;
}
}
if( ans > 0 ) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
  

  

牛客网暑期ACM多校训练营(第四场) G Maximum Mode 思维的更多相关文章

  1. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

  2. 牛客网暑期ACM多校训练营(第二场) D money 思维

    链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...

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

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

  4. 牛客网 暑期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 ,问你 ...

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

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

  6. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  7. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  8. 牛客网暑期ACM多校训练营(第九场)D

    链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...

  9. 牛客网暑期ACM多校训练营(第二场)B discount

    链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...

  10. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

随机推荐

  1. 机房ping监控 smokeping+prometheus+grafana(续) 自动获取各省省会可用IP

    一.前言 1.之前的文章中介绍了如何使用smokeping监控全国各省的网络情况:https://www.cnblogs.com/MrVolleyball/p/10062231.html 2.由于之前 ...

  2. RGB颜色 三者都是0为黑色而255是白色 解释

    问题: RGB颜色 都是0为黑色而255是白色 与日常生活的黑色白色差距怎么那么大,(与物理学中的黑色吸收光是否相悖)而且为什么要这样定义呢? 链接:https://www.zhihu.com/que ...

  3. 传输层的TCP和UDP协议

    作者:HerryLo 原文永久链接: https://github.com/AttemptWeb... TCP/IP协议, 你一定常常听到,其中TCP(Transmission Control Pro ...

  4. Micropython TPYBoard v102 温湿度短信通知器(基于SIM900A模块)

    前言 前段时间看了追龙2,感受就是如果你是冲着追龙1来看追龙2的话,劝你还是不要看了,因为追龙2跟追龙1压根没什么联系,给我的感觉就像是看拆弹专家似的,估计追龙2这个名字就是随便蹭蹭追龙1的热度来的. ...

  5. 2、大型项目的接口自动化实践记录--接口测试简介及RequestsLibrary关键字简介

    1.接口测试简介 1)先简单介绍下接口测试,那么什么是接口测试呢? 百科的回答:接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点. 看起来有 ...

  6. 【Java例题】7.2 线程题2-随机数求和线程

    2.随机数求和线程.设计一个线程子类,产生10000个随机数,并求和,显示和的结果:然后编写主类,在主函数中定义一个线程对象,并启动这个线程. package chapter7; public cla ...

  7. Cannot attach the file “MvcMovie.mdf” as database “aspnet-MvcMovie”

    今天在微软开发人员官网上学习asp.net mvc5入门的时候,遇到一个棘手的问题,我是按照教程一步一步操作的,但期间遇到一个自己觉得莫名其妙的问题,教程中也没有提到这个, 在添加新字段这一章节,跟着 ...

  8. MyBatis之foreach

    foreach foreach 元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内.它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符.这个元素是很智能的,它不会偶 ...

  9. JavaWeb——使用会话维持状态2

    在这次的例子里面,将完成一类似购物车的功能,在客户访问网站的时候,会选中自己将要购买的商品,而购物车将始终维持着商品的状态,会话将联系起选择第一个商品(第一个请求),选择其他商品(其他请求)以及付款等 ...

  10. [Spring cloud 一步步实现广告系统] 17. 根据流量类型查询广告

    广告检索服务 功能介绍 媒体方(手机APP打开的展示广告,走在路上看到的大屏幕广告等等) 请求数据对象实现 从上图我们可以看出,在媒体方向我们的广告检索系统发起请求的时候,请求中会有很多的请求参数信息 ...