链接: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. NYOJ 53 最少步数

    题      目    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=58 思路借鉴   DFS-Deep First Search-深度优先 ...

  2. Shiro权限框架与SpringMVC集成

    1.Shiro整合SpringMVC 我们学习Shiro框架肯定是要应用到Web项目上的,所以我们需要整合Shiro和SpringMVC 整合步骤: 第一步:SpringMVC框架的配置 spring ...

  3. Another option to bootup evidence files

    When it comes to booting up evidence files acquired from target disk, you got two options. One is VF ...

  4. .NET Core CSharp 中级篇 2-2 List,ArrayList和Dictionary

    .NET Core CSharp 中级篇 2-2 本节内容为List,ArrayList,和Dictionary 简介 在此前的文章中我们学习了数组的使用,但是数组有一个很大的问题就是存储空间不足,我 ...

  5. java高并发系列 - 第21天:java中的CAS操作,java并发的基石

    这是java高并发系列第21篇文章. 本文主要内容 从网站计数器实现中一步步引出CAS操作 介绍java中的CAS及CAS可能存在的问题 悲观锁和乐观锁的一些介绍及数据库乐观锁的一个常见示例 使用ja ...

  6. 湫湫系列故事——设计风景线 HDU - 4514

    题目链接:https://vjudge.net/problem/HDU-4514 题意:判断没有没有环,如果没有环,通俗的讲就是找出一条最长的路,相当于一笔画能画多长. 思路:dfs判环. 最后就是没 ...

  7. WPF:Task与事件在下载同步界面中的应用

    //设置一个下载事件类,可传输一个字符串  public class DownloadEventArgs:EventArgs     {         public string id { get; ...

  8. solidity智能合约字节数最大值及缩减字节数

    智能合约最大字节数 在Solidity中,EIP 170将contract的最大大小限制为24 KB .因此,如果智能合约内容过多,会导致无法进行发布操作. 减少压缩字节数方法 方法及变量命名 在一定 ...

  9. Usaco Training [1.3] wormhole

    传送门 解题要素:代码能力 解题步骤:理解题意 - >搜索枚举所有可能的配对情况 - >判断冲突并求解 - >调试 一. 理解题意 这里讲几个不容易理解的点: 1. +x方向 即向右 ...

  10. LeetCode_62_不同路径

    /** * @author jianw.li * @date 2019/1/22 11:11 PM * @Description: 不同路径 * 一个机器人位于一个 m x n 网格的左上角 (起始点 ...