news 2026/7/13 14:53:57

P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III

目录

    • 题目-P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III
    • 问题分析
    • 算法步骤
    • 代码实现

题目-P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III

问题分析

查询区间众数出现的次数, 尝试对区间进行分块

假设已经知道了区间内众数出现的次数s ss, 那么只需要判断散列块中是否有哪个数字还能成为众数

对于每个数字记录出现的位置v vv,v [ a [ i ] ] v[a[i]]v[a[i]]代表a [ i ] a[i]a[i]出现的一些位置,p o s [ i ] pos[i]pos[i]代表a [ i ] a[i]a[i]v [ a [ i ] ] v[a[i]]v[a[i]]中的索引

假设区间内众数出现的次数是a n s ansans

  • 枚举所有左侧散列块, 假设数值是w ww, 那么只需要检查是否存在v [ w ] [ p o s [ i ] + a n s ] ≤ r v[w][pos[i] + ans] \le rv[w][pos[i]+ans]r, 就可以更新a n s ansans, 也就是算上当前位置,w ww出现的次数是a n s + 1 ans + 1ans+1
  • 枚举右侧散列块, 假设数值是w ww, 检查v [ w ] [ p o s [ i ] − a n s ] ≥ l v[w][pos[i] - ans] \ge lv[w][pos[i]ans]l, 算上当前位置,w ww出现的次数是a n s + 1 ans + 1ans+1

算法步骤

  • 初始化分块, 对数据进行离散化
  • 暴力初始化f ff数组

代码实现

#include<bits/stdc++.h>usingnamespacestd;constintN=5e5+10,M=710;intn,m,a[N];vector<int>vec;intbsize,bcnt,st[M],ed[M],b[N];vector<int>v[N];intpos[N],f[M][M],tot[N];voidinit(){bsize=sqrt(n);bcnt=(n-1)/bsize+1;for(inti=1;i<=bcnt;++i){st[i]=(i-1)*bsize+1;ed[i]=min(n,i*bsize);}for(inti=1;i<=n;++i)b[i]=(i-1)/bsize+1;sort(vec.begin(),vec.end());vec.erase(unique(vec.begin(),vec.end()),vec.end());}intfind(intx){returnlower_bound(vec.begin(),vec.end(),x)-vec.begin();}intquery(intl,intr){intx=b[l],y=b[r];if(x==y){intans=0;for(inti=l;i<=r;++i)tot[a[i]]=0;for(inti=l;i<=r;++i)ans=max(ans,++tot[a[i]]);returnans;}intans=f[x+1][y-1];for(inti=l;i<=ed[x];++i){intt=pos[i];if(i+t<v[a[i]].size()&&v[a[i]][i+t]<=r)ans++;}for(inti=st[y];i<=r;++i){intt=pos[i];if(t-ans>=0&&v[a[i]][t-ans]>=l)ans++;}returnans;}intmain(){ios::sync_with_stdio(false);cin.tie(0);cin>>n>>m;for(inti=1;i<=n;++i)cin>>a[i],vec.push_back(a[i]);init();for(inti=1;i<=n;++i)a[i]=find(a[i]);for(inti=1;i<=n;++i){v[a[i]].push_back(i);pos[i]=v[a[i]].size();pos[i]--;}for(inti=1;i<=bcnt;++i){memset(tot,0,sizeoftot);for(intj=i;j<=bcnt;++j){f[i][j]=f[i][j-1];for(intk=st[j];k<=ed[j];++k){f[i][j]=max(f[i][j],++tot[a[k]]);}}}intlast=0;while(m--){intl,r;cin>>l>>r;l^=last,r^=last;if(r<l)swap(l,r);intans=query(l,r);cout<<ans<<'\n';last=ans;}return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/13 15:16:37

Python Wechaty:如何用9行代码打造专属智能微信助手?

Python Wechaty&#xff1a;如何用9行代码打造专属智能微信助手&#xff1f; 【免费下载链接】python-wechaty-getting-started Python Wechaty Starter Project Template that Works Out-of-the-Box 项目地址: https://gitcode.com/gh_mirrors/py/python-wechaty-getting-st…

作者头像 李华
网站建设 2026/7/14 12:26:22

终极指南:5步快速安装ChromeKeePass实现自动密码填充

终极指南&#xff1a;5步快速安装ChromeKeePass实现自动密码填充 【免费下载链接】ChromeKeePass Chrome extensions for automatically filling credentials from KeePass/KeeWeb 项目地址: https://gitcode.com/gh_mirrors/ch/ChromeKeePass 想要在Chrome浏览器中自动…

作者头像 李华
网站建设 2026/7/14 0:08:18

如何快速掌握ArtPlayer:构建专业级HTML5视频播放体验的完整指南

如何快速掌握ArtPlayer&#xff1a;构建专业级HTML5视频播放体验的完整指南 【免费下载链接】ArtPlayer :art: ArtPlayer.js is a modern and full featured HTML5 video player 项目地址: https://gitcode.com/gh_mirrors/ar/ArtPlayer ArtPlayer.js是一款专为现代Web应…

作者头像 李华
网站建设 2026/7/13 22:40:31

智能体开发1-智能体工具调用

智能体开发1-智能体工具调用 本教程先简要介绍智能体开发&#xff0c;在通过一个代码&#xff0c;实现智能体自动调用3个工具&#xff1a; 1.智能体根据需要自动读取网页数据并总结2.智能体可以读取本地表格数据并总结3.智能体会自己调用api读取股价数据并保存到本地 本文具体代…

作者头像 李华
网站建设 2026/7/14 10:47:14

GPU散热优化大师:打造个性化智能温控方案终极指南

GPU散热优化大师&#xff1a;打造个性化智能温控方案终极指南 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trending/fa/Fan…

作者头像 李华
网站建设 2026/7/13 15:09:40

鸿蒙与 Electron:跨平台开发的技术选型与实战融合

在跨平台开发的赛道上&#xff0c;鸿蒙&#xff08;HarmonyOS&#xff09; 和Electron是两个极具代表性的技术体系&#xff1a;鸿蒙以 “万物互联” 为核心&#xff0c;主打全场景分布式设备的协同开发&#xff1b;Electron 则依托 Web 技术栈&#xff0c;成为桌面跨平台应用开…

作者头像 李华