牛客网暑期ACM多校训练营(第四场) G Maximum Mode 思维
链接:https://www.nowcoder.com/acm/contest/142/G
来源:牛客网
输入描述:
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
-->
#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 思维的更多相关文章
- 牛客网暑期ACM多校训练营(第二场) I Car 思维
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...
- 牛客网暑期ACM多校训练营(第二场) D money 思维
链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期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 ,问你 ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
- 牛客网暑期ACM多校训练营(第九场)D
链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...
- 牛客网暑期ACM多校训练营(第二场)B discount
链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
随机推荐
- python创建虚拟环境(Windows)
>>>构建Python虚拟环境的目的是为了防止真实环境被破坏!!! >>>每一个项目建议用一个虚拟环境为了防止软件版本号冲突!!! 1.在终端切换到一个新的磁盘 如 ...
- WPF:window设置单一开启
方法一: Window window = new Window();window.ShowDialog; 方法二: 设置一个判断窗口打开状态的全局控制变量 private bool i ...
- mysql主从配置详解(图文)
最近工作不是很忙,把以前整理的mysql数据库的主从配置过程记录一下,有不足之处,请各位多多纠正指教 #环境配置#master IP:192.168.46.137 slave IP:192.168.4 ...
- 【科研民工笔记2】Ubuntu 16.04 安装nvidia驱动
我的主机是2060的显卡,用的是安装在U盘中的Ubuntu,开机进入后,因为没有安装驱动,所以界面看以来比较大. 通过手动方式,成功安装驱动,最终成功的方案使用的是run文件安装的方式. 1.手动下载 ...
- 不得不会的10点Java基础知识
1.实例变量和类变量 实例变量:指每个对象独立的,修改其中一个对象的实例变量,不会影响其他实例变量的值,变量值无 static 关键字修饰: 类变量:是指所有对象共享的,其中一个对象把该变量的值修改了 ...
- centos7之Python3.74安装
安装版本:Python3.74 系统版本:centos7 系统默认安装Python2.7,保留. 安装/usr/bin/Python3 安装需要root权限. 安装Python3的准备工作: 1.安装 ...
- 敏捷社区--敏捷与OKR
携程敏捷总动员是由携程技术管理中心(PMO)发起的敏捷项目管理线下主题沙龙活动(每2月一次),旨在和研发管理同行分享互联网行业第一线的优秀敏捷实践. 5月10日携程敏捷总动员-OKR专场活动, ...
- C#使用NLOG System.TypeInitializationException,类型初始值设定项引发异常
C#如何使用NLOG,网上有很多介绍,本次使用时遇到一个问题,使用NLOG写日志时,出现初始化异常,基本异常信息如下: System.AggregateException: 发生一个或多个错误. -- ...
- Nginx总结(一)Linux如何安装Nginx
以前写过一些Nginx的文章,但都是用到什么说什么,没有一个完整系统的总结.趁最近有时间,打算将Nginx相关的内容重新整理一下.nginx系列文章地址如下:https://www.cnblogs.c ...
- 纯 CSS 实现绘制各种三角形(各种角度)
一.前言 三角形实现原理:宽度width为0:height为0:(1)有一条横竖边(上下左右)的设置为border-方向:长度 solid red,这个画的就是底部的直线.其他边使用border-方向 ...