luogu题解 P3709 【大爷的字符串题】
题目链接:
思路:
首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数。
然后肯定是用莫队,具体怎么写莫队其他题解都写得很详细,这里不赘述.然后观察数据范围1e9肯定要离散化。
但是题解里讲离散化的不多,我就讲一讲我自己瞎搞的一个离散化方法
看到题解里其他dalao都是什么lower_bound或我看不懂的神仙操作。而蒟蒻我就瞎搞出了一个比较暴力的,也通俗易懂方法---开了两个map.
第一个map al用来离散化,记录元素是否出现过.
第二个map getrk顾名思义,用来离散化获取每个数的排名.
dat[]用来记录原字符串,num[]用来记录去重后的数,即所有出现的元素
读入字符串后将num[]从大到小排序,记录每一个出现元素的排名,当然就是用getrk
接着就不管num[],我们遍历一遍获取原字符串中每个数的排名完成离散化.
由于开了map,常数比较大,开了O2跑了2000多ms,但是能过就行了
代码:
// luogu-judger-enable-o2
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
using namespace std;
const int maxn=200005;
map <int,bool>al;
map <int,int>getrk;
struct Ask{
int l,r,id,x;
}ask[maxn];
int num[maxn],dat[maxn],rk[maxn];
int belong[maxn],block;
int N,n=0,m;
int a[maxn],cnt[maxn];
int ans[maxn],anss=0;
template<class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;
return ;
}
inline bool cmp0(const int&x,const int&y){
return x>y;
}
inline bool cmp2(const Ask &x,const Ask &y){
return belong[x.l]^belong[y.l]?belong[x.l]<belong[y.l]:belong[x.l]&1?x.r<y.r:x.r>y.r;
}
inline void init()
{
int x;
read(N),read(m);
block=N/sqrt(m*2/3);
n=0;
for(register int i=1;i<=N;i++){//N是原字符串长度
read(x);
dat[i]=x;
if(al[x]==0){//去重
num[++n]=x;//n是去重后num[]数组长度
al[x]=1;
}
belong[i]=(i-1)/block+1;
}
sort(num+1,num+1+n,cmp0);
for(register int i=1;i<=n;i++){
getrk[num[i]]=i;
// belong[i]=(i-1)/block+1;
}
for(register int i=1;i<=N;i++){
rk[i]=getrk[dat[i]];
}
//sort(num+1,num+1+n,cmp1);
for(register int i=1;i<=m;i++){
read(ask[i].l),read(ask[i].r);
ask[i].id=i;
}
sort(ask+1,ask+1+m,cmp2);
return;
}
inline void add(int x){
int now=rk[x];
if(anss==a[now])anss++;
cnt[a[now]]--;
a[now]++;
cnt[a[now]]++;
return;
}
inline void sub(int x){
int now=rk[x];
if(anss==a[now]&&cnt[a[now]]==1)anss--;
cnt[a[now]]--;
a[now]--;
cnt[a[now]]++;
return;
}
inline void solve()
{
int l=1,r=0,ll,rr;
cnt[0]=n;
for(register int i=1;i<=m;i++){
ll=ask[i].l,rr=ask[i].r;
while(r<rr)add(++r);
while(r>rr)sub(r),r--;
while(l<ll)sub(l),l++;
while(l>ll)add(--l);
ans[ask[i].id]=anss;
}
for(register int i=1;i<=m;i++){
printf("-%d\n",ans[i]);
}
return ;
}
int main()
{
init();
solve();
return 0;
}
luogu题解 P3709 【大爷的字符串题】的更多相关文章
- 【Luogu】P3709大爷的字符串题(莫队算法)
题目链接 语文题啊…… 看题解发现是让求区间中最多的数的个数,于是果断理解了一会题解……莫队套上完事. sum[i]表示i这个数出现的次数,cnt[i]表示出现i次的数有几个,然后乱搞搞……就好了 # ...
- luogu P3709 大爷的字符串题
二次联通门 : luogu P3709 大爷的字符串题 /* luogu P3709 大爷的字符串题 莫队 看了半天题目 + 题解 才弄懂了要求什么... 维护两个数组 一个记录数字i出现了几次 一个 ...
- P3709 大爷的字符串题 (莫队)
题目 P3709 大爷的字符串题 题意:求\([l,r]\)中众数的个数. 解析 维护两个数组: \(cnt[x]\),数\(x\)出现的次数. \(sum[x]\),出现次数为\(x\)的数的个数. ...
- P3709 大爷的字符串题(莫队+结论)
题目 P3709 大爷的字符串题 做法 有一个显然的结论:一段区间里最小答案为众数的个数 用莫队来离线求众数 \(tmp_i\)表示出现\(i\)次的数的个数,\(num_i\)表示\(i\)出现的次 ...
- 洛谷 P3709 大爷的字符串题
https://www.luogu.org/problem/show?pid=3709 题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个 ...
- 洛谷P3709 大爷的字符串题(莫队)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- P3709 大爷的字符串题(50分)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- 【luogu P3709 大爷的字符串题】 题解
题目链接:https://www.luogu.org/problemnew/show/P3709 离散化+区间众数..? #include <iostream> #include < ...
- 【题解】洛谷P3709大爷的字符串题
最近想要练习一下莫队(实在是掌握的太不熟练了啊.)这题一开始看到有点懵(题面杀),后来发现是要求众数的个数.乍一看好像很难的样子. 但仔细分析一下:首先往序列当中加入一个数,这个是很简单的,只需要维护 ...
- 并不对劲的p3709:大爷的字符串题
题目大意 区间众数 题解 莫队 代码 #include<algorithm> #include<cmath> #include<cstdio> #include&l ...
随机推荐
- debian上搭建私有docker仓库
docker官方仓库是docker hub.虽然很好用,但是无法满足私密性的要求. 如果只需要在局域网内或者朋友圈内分享各自制作的image,那么,搭建属于自己的docker仓库变得很有必要. 一.环 ...
- BrowserUtils
import android.content.Context; import android.content.Intent; import android.net.Uri; public class ...
- Django博客系统
零.创建项目及配置 一.编写 Model 层的代码 二.配置 admin 页面 三.根据需求定制 admin
- python调试工具remote_pdb
介绍一个调试python代码的工具:remote_pdb https://pypi.org/project/remote-pdb/ 安装 pip install remote-pdb 使用 1,设置断 ...
- Centos安装openjdk
转载自:https://blog.csdn.net/youzhouliu/article/details/51183115 openjdk在linux各个平台下安装源中可以找到. 命令查找安装源中有什 ...
- 安装flanal报错解决
1.:Error registering network: failed to acquire lease: node "test4" pod cidr not assigned ...
- html5 iphone input 输入法 弹窗将页面顶起 解决办法
给 input 添加失焦事件,然后滚动视图 input.search(type="text",@blur="scrollTop") scrollTop(){ w ...
- C# 保留两位“有效数字”,而不是两位“小数”
1.问题描述: 最近在处理软件结果显示时,发现如果利用 Math.Round(Number,N) 取N为小数时,有的结果不能显示完全 比如:15.3245 和 0.00106 两个数字,如果 N=2 ...
- 20190526 - CentOS 7 中 安装 MySQL 8 并授权 root 远程访问
1. CentOS 7 中 安装 MySQL 8 CentOS 7 中内置 MariaDB 建议升级一下用,性能好很多.但如果一定要用 MySQL 8,就得自己装. 坦白的说,Oracle 升级 My ...
- 单点登录之CAS原理和实现(转载)
转载源:https://www.jianshu.com/p/613c615b7ef1 单点登录之CAS原理和实现 来源于作者刘欣的<码农翻身> + 自己的备注理解 这家集团公司财大气粗,竟 ...