牛客网暑期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,使得对所 ...
随机推荐
- Git应用之eclipse解决冲突代码
最近上班公司框架换成了微服务下面是eclipse 对代码进行管理 1.冲突代码 如果两个人在一个项目上同一文件上更改代码就会出现冲突现象 先用NewFile.jsp 文件做演示 打开eclipse从 ...
- 简易数据分析 09 | Web Scraper 自动控制抓取数量 & Web Scraper 父子选择器
这是简易数据分析系列的第 9 篇文章. 今天我们说说 Web Scraper 的一些小功能:自动控制 Web Scraper 抓取数量和 Web Scraper 的父子选择器. 如何只抓取前 100 ...
- 对比度拉伸(一些基本的灰度变换函数)基本原理及Python实现
1. 基本原理 对比度拉伸是扩展图像灰度级动态范围的处理.通过在灰度级中确定两个点来控制变换函数的形状.下面是对比度拉伸函数中阈值处理的代码示例,阈值为平均值. 2. 测试结果 图源自skimage ...
- GDOI#348大陆争霸[SDOI2010]最短路有限制条件
在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的 克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭 的神曾·布拉泽,而克里斯国信仰象征光明和永恒的神斯普林·布拉 ...
- HTML/CSS:div水平与元素垂直居中(2)
单个div水平居中:设置margin的左右边距为自动 div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中 ...
- Android Bluetooth Low Energy (BLE)简单方便的蓝牙开源库——EasyBLE
源码传送门 最新版本 功能 支持多设备同时连接 支持广播包解析 支持连接同时配对 支持搜索系统已连接设备 支持搜索器设置 支持自定义搜索过滤条件 支持自动重连.最大重连次数限制.直接重连或搜索到设备再 ...
- 【KakaJSON手册】02_JSON转Model_02_数据类型
由于JSON格式的能表达的数据类型是比较有限的,所以服务器返回的JSON数据有时无法自动转换成客户端想要的数据类型. 比如服务器返回的时间可能是个毫秒数1565480696,但客户端想要的是Date类 ...
- DNS解析综合学习案例
DNS解析综合学习案例 #图右侧为做题前环境配置 #命令为红色 #命令加载内容为绿色 #vi编辑内容为蓝色 1.用户需把/dev/myvg/mylv逻辑卷以支持磁盘配额的方式挂载到网页目录下 [roo ...
- element-ui表单验证无效解决
最近在项目中遇到了一个需求,需要动态增减表单元素,同时给新增的表单元素增加校验规则. element-ui官网给出了解决方案:点击新增按钮时,向循环渲染的数组中push新的对象,数据驱动视图,通过增加 ...
- net core Webapi 总目录
目录 博客目录 代码地址 博客目录 net core Webapi基础工程搭建(一)--开发工具及环境 net core Webapi基础工程搭建(二)--创建工程 net core Webapi基础 ...