[ Luogu 3709 ] 大爷的字符串题
\(\\\)
Description
原题题面太过混乱出题人语文凉凉
给出一个长为 \(n\) 的数列 \(A\) ,多次询问:
对于一个区间 \([L_i,R_i]\),把区间内的所有数最少划分成多少个数集,使得每一个集合内没有相同元素。
- \(A_i\le 10^9,n,m\le 2\times 10^5\)
\(\\\)
Solution
题目的模型很容易转化成区间众数问题。
莫队求解区间众数。
首先数据范围是假的,离散化之后就开的下桶了。
对于区间扩张,肯定是加一下桶,然后跟当前答案取 \(max\) 。
问题在于区间缩小时,删除一个数怎么搞。
\(\\\)
开始有一个 too simple 想法,是用堆去维护当前答案区间内所有数出现个数,然后懒惰删除法,每次更新时判断一下堆顶是否正确。
想一想是对的,但是 \(O(N\sqrt NlogN)\) 的复杂度对于 \(2\times 10^5\) 很吃力。
\(\\\)
一个机智的做法。
设 \(cnt[i]\) 表示 \(bkt[x]=i\) 的个数,也就是当前区间里出现次数为 \(i\) 的数的个数。
空间没有问题,因为最多出现数列长度的次数。
这样一来修改就容易了很多,减的时候只需要判断一下,当前数对应的 \(cnt\) 是否 \(>1\) 即可。
注意加减是对 \(cnt\) 和 \(bkt\) 的同时更新。
\(\\\)
Code
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 200000
#define R register
#define gc getchar
using namespace std;
inline int rd(){
int x=0; bool f=0; char c=gc();
while(!isdigit(c)){if(c=='-')f=1;c=gc();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
return f?-x:x;
}
int n,m,ans,bl[N],cnt[N],bkt[N],s[N],tmp[N];
struct Q{int l,r,ans,id;}q[N];
inline bool cmp1(Q x,Q y){
return bl[x.l]==bl[y.l]?x.r<y.r:bl[x.l]<bl[y.l];
}
inline bool cmp2(Q x,Q y){return x.id<y.id;}
inline void add(int p){
--cnt[bkt[s[p]]];
++cnt[++bkt[s[p]]];
ans=max(ans,bkt[s[p]]);
}
inline void del(int p){
--cnt[bkt[s[p]]];
if(ans==bkt[s[p]]&&!cnt[bkt[s[p]]]) --ans;
++cnt[--bkt[s[p]]];
}
int main(){
n=rd(); m=rd();
int t=sqrt(n),tot=0;
for(R int i=1,cntt=1;i<=n;++i){
tmp[i]=s[i]=rd();
if(i%t==0) ++cntt;
bl[i]=cntt;
}
sort(tmp+1,tmp+1+n);
for(R int i=1;i<=n;++i){
tmp[++tot]=tmp[i];
while(tmp[i+1]==tmp[i]&&i<=n) ++i;
}
for(R int i=1;i<=n;++i) s[i]=lower_bound(tmp+1,tmp+1+tot,s[i])-tmp;
for(R int i=1;i<=m;++i){
q[i].l=rd(); q[i].r=rd(); q[i].id=i;
}
sort(q+1,q+1+m,cmp1);
bkt[s[1]]=cnt[1]=ans=1;
int nowl=1,nowr=1;
for(R int i=1;i<=m;++i){
while(nowl<q[i].l){del(nowl);++nowl;}
while(nowl>q[i].l){--nowl;add(nowl);}
while(nowr>q[i].r){del(nowr);--nowr;}
while(nowr<q[i].r){++nowr;add(nowr);}
q[i].ans=ans;
}
sort(q+1,q+1+m,cmp2);
for(R int i=1;i<=m;++i) printf("%d\n",-q[i].ans);
return 0;
}
[ Luogu 3709 ] 大爷的字符串题的更多相关文章
- luogu 3709 大爷的字符串题 构造 莫队 区间众数
题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...
- luogu P3709 大爷的字符串题
二次联通门 : luogu P3709 大爷的字符串题 /* luogu P3709 大爷的字符串题 莫队 看了半天题目 + 题解 才弄懂了要求什么... 维护两个数组 一个记录数字i出现了几次 一个 ...
- 【luogu P3709 大爷的字符串题】 题解
题目链接:https://www.luogu.org/problemnew/show/P3709 离散化+区间众数..? #include <iostream> #include < ...
- P3709 大爷的字符串题 (莫队)
题目 P3709 大爷的字符串题 题意:求\([l,r]\)中众数的个数. 解析 维护两个数组: \(cnt[x]\),数\(x\)出现的次数. \(sum[x]\),出现次数为\(x\)的数的个数. ...
- AC日记——大爷的字符串题 洛谷 P3709
大爷的字符串题 思路: 莫队,需开O2,不开50: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 20000 ...
- P3709 大爷的字符串题(莫队+结论)
题目 P3709 大爷的字符串题 做法 有一个显然的结论:一段区间里最小答案为众数的个数 用莫队来离线求众数 \(tmp_i\)表示出现\(i\)次的数的个数,\(num_i\)表示\(i\)出现的次 ...
- 洛谷 P3709 大爷的字符串题
https://www.luogu.org/problem/show?pid=3709 题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个 ...
- 洛谷P3709 大爷的字符串题(莫队)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- P3709 大爷的字符串题(50分)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
随机推荐
- HDU 5666 Segment
Segment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- asp.net下的cookieName
https://stackoverflow.com/questions/1017144/rename-asp-net-sessionid Add to your web.config:- <sy ...
- UIButton常见属性和方法
一.创建,两种方法: 1. 常规的 initWithFrame UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 8 ...
- CodeForces-607B:Zuma (基础区间DP)
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- Autolayout UIScrollView
http://www.cocoachina.com/ios/20141011/9871.html Xcode6中如何对scrollview进行自动布局(autolayout) Xcode6中极大的 ...
- C++初学(1) 简单的加减乘除取余运算代码
//---------------+-*/%算法---------------------------------------------------------- #include <iost ...
- POJ2488【DFS】
阿西吧,搞清楚谁是行,谁是列啊!!! #include <stdio.h> #include <string.h> #include <math.h> #inclu ...
- class JsonItemExporter(BaseItemExporter):
class JsonItemExporter(BaseItemExporter):这个类的实现和几年前的实现有了点小变化,主要就是是否添加换行
- memcached原理及介绍
memcached是一种缓存技术,在存储在内存中(高性能分布式内存缓存服务器).目的 : 提速.(传统的都是把数据保存在关系型数据库管理系统既RDBMS,客户端请求时会从RDBS中读取数据并在浏览器中 ...
- PHP + jquery.validate remote的用法
[ 转 ] http://www.cnlvzi.com/index.php/Index/article/id/58 最近做验证功能时,用到jquery.validate.js中的remote远程验证方 ...