Maximum Mode【Nowcoder多校训练第4场G题】(离散化+二分)

离散化详解+例题+程序 NOIP离散化是考试内容,很多同学对离散化并不理解,本文通过非常多的例题由浅入深的讲解,看完必会。 立即下载

来源:牛客网

题目描述
The mode of an integer sequence is the value that appears most often. Chiaki has n integersa1,a2,...,ana1,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≤105,0≤m<n)(1≤n≤105,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≤109)a1,a2,...,an(1≤ai≤109) denoting the sequence.
It is guaranteed that the sum of all n does not exceed 106106.
输出描述:
For each test case, output an integer denoting the only maximum mode, or -1 if Chiaki cannot achieve it.
示例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
我的做法好像有点复杂啊。先离散化,然后,统计每个离散化后出现的数字numnum的个数,再给这些个数countcount从小大排序,从numnum最大的开始验证,验证是如果答案是numinumi,那么个数设为counticounti,那么所以比counticounti大的值都必须降低到counti−1counti−1,这是最低值,那么我们就可以验证了,这里就是二分在加上前缀和了
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#define maxx 100005
using namespace std;
int n,m;
int a[maxx];
int id[maxx];
int cnt,num;
int get(int x)
{
    int l=1,r=num;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(id[mid]==x) return mid;
        if(id[mid]<x) l=mid+1;
        else r=mid-1;
    }
    return 0;
}
int b[maxx];
int order[maxx];
int sum[maxx];
int tot;
int _get(int x)
{
    int l=1,r=tot;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(order[mid]>=x) r=mid-1;
        else l=mid+1;
    }
    return l;
}
void init()
{
    cnt=0;
    tot=0;
    memset(b,0,sizeof(b));
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",a+i),id[++cnt]=a[i];
        num=unique(id+1,id+cnt+1)-id-1;
        sort(id+1,id+num+1);
        for(int i=0;i<n;i++) a[i]=get(a[i]);//离散化
        for(int i=0;i<n;i++) b[a[i]]++;//统计个数
        for(int i=1;i<=num;i++)
            if(b[i])order[++tot]=b[i];//把个数收集起来
        sort(order+1,order+1+tot);
        for(int i=1;i<=tot;i++)
            sum[i]=sum[i-1]+order[i];//前缀和
        bool sign=false;
        int ans;
        for(int i=num;i>0;i--)
        {
            int index=_get(b[i]);//二分去找边界,就是找到所有大于等于b[i]的最小值
            int total=sum[tot]-sum[index];
            if(total-(b[i]-1)*(tot-index)<=m)//验证
            {
                //cout<<"xixi: "<<total-(b[i]-1)*(tot-index)<<endl;
                ans=id[i];
                sign=true;
                break;
            }
        }
        if(sign)printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}
FastGPT 引申:借鉴 FastGPT 基于MySQL + ES 实现知识库(含表结构以及核心代码) 阅读详情

相关推荐

【cocos2dx面试题干货】--2021年最新cocos2dx面试干货(引擎篇)

【cocos2dx面试题干货】--2020年度最新cocos2dx面试干货(引擎篇 ) 大家好,我是Lampard~~ 经过春招一番艰苦奋战之后,我终于是进入了心仪的公司。 今天给大家分享一下我在之前精心准备的一套面试知识。 由于我面试的岗位是cocos2dx的开发岗,今天就先给大家分享一下引擎方面的知识。 本人亲测80%的引擎相关题目都...

Lampard的博客 1万+

2018 牛客多校四 G Maximum Mode (暴力枚举)

题目 题意:给你n个数,然后删除m个数,最后保证众数只有一个且尽量让他打,如果存在输出那个数,不存在输出-1. 思路:就是统计每个数出现的次数,然后按照每个数出现次数从小到大排序,然后暴力枚举每个数为答案时,需要删除几个数,因为从小到大的,我们在枚举这个数为所求答案时,只要减去后缀和即可,然后通过二分枚举第一个出现这个次数的位置,只需这个位置到最后面都减到比枚举这个数出现次数小时就可以了,然后...

Oops的博客 274

服务器被黑?快速检测和识别系统中的恶意进程

在管理服务器时,检测和识别系统中的恶意进程至关重要。本文介绍了使用 `ps`、`top`、`htop` 等系统监控工具,分析日志文件,以及使用 `chkrootkit` 和 `rkhunter` 等安全工具的方法。通过这些步骤,可以有效地检测和处理恶意进程,确保系统的安全和稳定。

_midnight的博客 1420

(第四场)G Maximum Mode 【YY+暴力】

链接: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 e...

weixin_30576827的博客 170

省一蓝桥杯嵌入式-09RTC实时时钟

RTC(Real-Time Clock)实时时钟,是一个独立的定时器RTC模块拥有一组连续的计数的计数器,在响应软件配置下,可提供时钟、日历、闹钟功能RTC能提供年月日时分秒等信息(可设置)在系统复位或从待机模式唤醒后,RTC的设置时间维持不变(这个我没实现,复位后时间也重置了,有能力的可以自己研究一下)持续时间跟踪:在主系统断电后或处于低功耗模式时,RTC仍继续工作,原理是有额外的电池为其供电(蓝桥杯板子没有这个备用电源,该功能无效。

qq_46392331的博客 1733

【实战】S7-PLCSIM Advanced V3.0 授权问题排查指南(Error Code:-30与LicenseNotFound深度解析)

本文深度解析S7-PLCSIM Advanced V3.0启动时遇到的Error Code:-30与LicenseNotFound报错问题。文章提供了一套系统性的排查指南,从检查WinPcap驱动兼容性、确保Automation License Manager服务运行,到正确安装浮动授权以及清理系统残留缓存,逐步解决授权链条中的各个环节故障,帮助用户彻底解决仿真环境启动失败的问题。

weixin_29185167的博客 732

Maximum Mode

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

lml11111的博客 261

2023.03.27更新大麦网PC端抢购软件和使用教程

近期发现大麦网页端抢购页面悄然发生变化,之前可以在页面选择票价、档次,并且点击按钮进行购票,现在不行了,新的页面如下图所示。已经安全没有了点击按钮,因此,但是并不代表不能像以前之前那样。

猫敷雪 1万+

2018牛客多校训练-----Maximum Mode(贪心)

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

jinghui_7的博客 283

什么是漏洞管理?为什么它很重要?(非常详细)

2. **漏洞扫描程序**:漏洞扫描程序通常通过对系统和网络进行一系列测试来寻找常见的弱点或缺陷,如尝试利用已知漏洞、猜测默认密码或用户帐户,或者只是尝试访问受限区域。1. **防范网络威胁**:漏洞管理在恶意行为者利用漏洞之前识别和纠正漏洞方面发挥着关键作用,有助于降低网络攻击、数据泄露以及随后的财务和声誉损失的风险。5. **安全信息和事件管理(SIEM)**:SIEM软件可实时整合组织的安全信息和事件,监视网络流量、识别试图连接到内部系统的设备、跟踪用户活动等。

guo162308的博客 273

国科大学习资料--模式识别与机器学习(黄庆明)--期末复习题2(含答案)

国科大学习资料–模式识别与机器学习(黄庆明)–期末复习题2(含答案) 国科大学习资料–模式识别与机器学习(黄庆明)–期末复习题2(含答案) 国科大学习资料–模式识别与机器学习(黄庆明)–期末复习题2(含答案) ...

无名何许人的博客 1万+

牛客网多校训练赛Maximum Mode(思维)

题目链接https://www.nowcoder.com/acm/contest/142/G 题意求n个数中删去m个数能得到的最大众数,若无则输出-1 思路:枚举最大众数的值,看是否能得到该结果。 #include &lt;iostream&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt;...

reallsp的博客 333

UG NX二次开发(C#)-读取PMI对象的名称

摘要:本文介绍了在UG NX中通过NXOpen获取PMI对象名称的方法。首先说明PMI对象初始无名称,可通过重命名或属性设置名称。然后提供C#代码示例,展示如何遍历PMI集合获取对象名称,并显示名称和类型信息。适用于需要操作PMI对象的开发人员,欢迎加入技术交流QQ群749492565。(99字)

GimiGimmy 601

代码段时间 python_最节省时间的有用python代码段

代码段时间 python 程式设计 (PROGRAMMING) Here are my favorite 29 Python snippets that actually save me time as a developer 这是我最喜欢的29个Python代码段,它们实际上为我节省了开发人员的时间 Python is one of the most popular languages ​​u...

weixin_26711425的博客 335

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

题目描述  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 mod...

snayf的博客 203

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

离散化

  本来应该是很简单的东西,但是之前学长讲的时候也没怎么听,然后现在遇到需要离散化的题目就有点茫然了。看了下网上大佬们的博客,基本理解了,做个记录。   以下内容部分思路来自:   https://blog.csdn.net/xiangaccepted/article/details/73276826      离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率...

weixin_30388677的博客 344

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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

链接:https://www.nowcoder.com/acm/contest/142/G 来源:牛客网   Maximum Mode 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 The mode of an integer sequence is the value that...

qq_41668093的博客 306

C++编程,数据结构,算法类面试题集

1. 在linked list中找倒数第N个结点 2. 倒转linked list 3. 二叉树的结点有指向parent的指针,求最近公共祖先 4. 给一个数组,如何打印该数组成员构成集合的全部子集合. 5. 有两个字符串,一个是text,一个是command, Command有四种:    ‘+’: 在text中前进一位    ‘-’: 在text中后退一位    ‘a’: 在当前

rongwenbin的专栏 1万+

20211101

目录 A - Era 题意: 思路: 代码: B - XOR Specia-LIS-t 题意: 思路: 代码: C - Di-visible Confusion 题意: 思路: 代码: D - Moderate Modular Mode 题意: 思路: 代码: A - Era Shohag has an integer sequencea_1, a_2, \ldots, a_na1​,a2​,…,an​. He can perform the following o.

wsmbkbhzw_的博客 644

基础思维——离散化(附例题)

离散化: 把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。具体来说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。

Zhengyangxinn的博客 1720

牛客多校训练营Maximum Mode

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

Pandapan1997的博客 354

离散化入门

离散化

unknown 1292

牛客第四次多校Maximum Mode

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

weixin_30578677的博客 186

离散化详解

离散化:把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb

a_forever_dream的博客 1464

Best Cow Fences POJ - 2018(经典二分)

Farmer John’s farm consists of a long row of N (1 &lt;= N &lt;= 100,000)fields. Each field contains a certain number of cows, 1 &lt;= ncows &lt;= 2000. FJ wants to build a fence around a contiguous ...

Coldfresh的博客 2077
上一篇: 【HDU 5608】function(杜教筛)
下一篇: 【51nod 1238】 最小公倍数之和 V3(杜教筛)
coldfresh
博客等级 码龄10年 107粉丝 405原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值