给出一个跑得快一点的做法,洛谷最优解 (时间是第二名的 \(\frac{1}{2}\)), CF 第一页

D1

首先找到整个序列的众数 \(G\), 很容易证明答案序列中的两个众数中其中一个是 \(G\) 。

知道了这个结论以后,我们可以枚举在序列中出现的数 \(K\), 让 \(G\) 的权值为 \(1\), \(K\) 的权值为 \(-1\), 然后就找一下最长的权值为 \(0\) 的串即可。这个开个桶统计即可。

这个和大家一样,就不多说了。

Code(片段) :

const int N = 2e5 + 7;
int n, a[N], cnt[N], zs, ans, fir[N << 1];
void work(int x) {
int now = N;
memset(fir, -1, sizeof(fir));
fir[now] = 0;
L(i, 1, n) {
if(a[i] == zs) now ++;
else if(a[i] == x) now --;
if(fir[now] == -1) fir[now] = i;
else ans = max(ans, i - fir[now]);
}
}
int main() {
scanf("%d", &n);
L(i, 1, n) scanf("%d", &a[i]), cnt[a[i]] ++;
L(i, 1, n) if(cnt[i] > cnt[zs]) zs = i;
L(i, 1, min(n, 100)) if(i != zs) work(i);
printf("%d\n", ans);
return 0;
}

D2

同样令众数为 \(G\)。

根号分治。

对于出现次数 \(> B\) 的数,可以像 \(D1\) 一样处理。

对于出现次数 \(\le B\) 的数 (设为 \(K\))(重点):

设出现次数为 \(cnt\)。

首先可以枚举选中的序列的第一个出现 \(K\) 的位置是 \(K\) 的第几次出现的位置。

然后发现这个序列中包含的 \(G\) 的个数一定 \(\le cnt\)。

于是我们可以只考虑枚举的这个位置前面的 \(cnt\) 个 \(G\) (不能包含上一个数字 \(K\)) 和后面 \(cnt\) 个 \(G\) (可以包含后面的数字 \(K\)) ,然后按照 \(D1\) 的方法做即可。

有一些细节,具体见代码。

Code :

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int, int>
#define mkp make_pair
using namespace std;
char buf[256],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,256,stdin),p1==p2)?EOF:*p1++)
inline int read() {
int x = 0, f = 1; char ch = getchar();
while(!isdigit(ch)) { if(ch=='-') f = -1; ch = getchar(); }
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
const int N = 2e5 + 7;
const int B = 233;
int n, a[N], cnt[N], zs, ans;
int fir[N << 1];
int max(int x, int y) { return x > y ? x : y; }
void worka(int x) {
int now = N;
memset(fir, -1, sizeof(fir));
fir[now] = 0;
L(i, 1, n) {
if(a[i] == zs) now ++;
else if(a[i] == x) now --;
if(!~fir[now]) fir[now] = i;
else ans = max(ans, i - fir[now]);
}
}
int lef[N], rig[N], f[N], fg[N];
vector<int> ve[N];
void workb(int x) {
L(i, 1, cnt[x]) {
fill(fir + N - cnt[x] - 2, fir + N + cnt[x] * 2 + 3, -1);
int tot = 0, las = (i == 1 ? 0 : ve[x][i - 2]), now = ve[x][i - 1], len = 0;
while(lef[now - 1] > las && len <= cnt[x]) now = lef[now - 1], ++len, f[++tot] = now;
int dd = i, KK = N;
if(!lef[now - 1] && i == 1) fir[N] = 0;
reverse(f + 1, f + tot + 1);
f[++tot] = ve[x][i - 1], fg[tot] = 1;
now = ve[x][i - 1], len = 0;
while(rig[now + 1] && len <= cnt[x]) {
now = rig[now + 1];
while(dd < cnt[x] && ve[x][dd] < now) f[++tot] = ve[x][dd], fg[tot] = 1, ++ dd;
++len, f[++tot] = now;
}
if(len <= cnt[x]) while(dd < cnt[x]) f[++tot] = ve[x][dd], fg[tot] = 1, ++ dd;
f[tot + 1] = n + 1;
if(rig[now + 1]) f[tot + 1] = rig[now + 1];
L(j, 1, tot) {
if(fg[j] == 1) -- KK, fg[j] = 0; else ++ KK;
if(!~fir[KK]) fir[KK] = f[j];
else ans = max(ans, f[j + 1] - 1 - fir[KK]);
}
}
}
int main() {
n = read();
L(i, 1, n) a[i] = read(), cnt[a[i]] ++;
L(i, 1, n) if(cnt[i] > cnt[zs]) zs = i;
L(i, 1, n) if(a[i] == zs) lef[i] = rig[i] = i;
L(i, 1, n) if(!lef[i]) lef[i] = lef[i - 1];
R(i, n, 1) if(!rig[i]) rig[i] = rig[i + 1];
L(i, 1, n) if(cnt[a[i]] <= B) ve[a[i]].push_back(i);
L(i, 1, n) if(i != zs) {
if(cnt[i] > B) worka(i);
else workb(i);
}
printf("%d\n", ans);
return 0;
}

题解 CF1446D2 【Frequency Problem (Hard Version)】的更多相关文章

  1. Codeforces 1446D2 - Frequency Problem (Hard Version)(根分)

    Codeforces 题面传送门 & 洛谷题面传送门 人菜结论题做不动/kk 首先考虑此题一个非常关键的结论:我们设整个数列的众数为 \(G\),那么在最优子段中,\(G\) 一定是该子段的众 ...

  2. Possible concurrency problem: Replicated version id X matches in-memory version for session ...

    The message basically is saying that a replicated session is overriding an existing session in that ...

  3. 【题解】Tree-String Problem Codeforces 291E AC自动机

    Prelude 传送到Codeforces:(/ω\)--- (/ω•\) Solution 很水的一道题. 对查询的串建出来AC自动机,然后树上随便跑跑就行了. 为什么要写这篇题解呢? 我第一眼看到 ...

  4. Description Resource Path Location Type Java compiler level does not match the version of the installed Java project facet Unknown Faceted Project Problem (Java Version Mismatch)

    project 编译问题,需要三处的jdk版本要保持一致,才能编译通过. 1.在项目上右键properties->project Facets->修改右侧的version  保持一致 2. ...

  5. P1832题解 A+B Problem(再升级)

    万能的打表 既然说到素数,必须先打素数表筛出素数, 每个素数可以无限取,这就是完全背包了. 这次打个质数表: bool b[1001]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1 ...

  6. 题解 CF1428G Lucky Numbers (Easy Version and Hard Version)

    这题没有压行就成 \(\texttt{Hard Version}\) 最短代码解了( 要知道这题那么 \(sb\) 就不啃 \(D\) 和 \(E\) 了. \(\texttt{Solution}\) ...

  7. 题解:T103342 Problem A. 最近公共祖先

    题目链接 题目大意 求每个点对的lca深度的和 以每一层分析,得出通式 由于1e9的数据范围要化简表达式得到O(能过) 瞎搞后就是2^(2n+2)-(4n+2)*2^n-2 code: #includ ...

  8. 多校联训 DS 专题

    CF1039D You Are Given a Tree 容易发现,当 \(k\) 不断增大时,答案不断减小,且 \(k\) 的答案不超过 \(\lfloor\frac {n}{k}\rfloor\) ...

  9. 记一次jdk升级引起的 Unsupported major.minor version 51.0

    之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...

随机推荐

  1. nginx&http 第二章 ngx启动多进程

    Nginx服务器使用 master/worker 多进程模式. 主进程(Master process)启动后,会接收和处理外部信号: 主进程启动后通过fork() 函数产生一个或多个子进程(work ...

  2. uniapp 证书 打包上线GooglePlay app自动升级

    uniapp Android证书 打包上线GooglePlay app自动升级 1.Android证书申请 要安装jdk并配置环境变量. keytool -genkey -alias android ...

  3. hectf2020部分简单题题解wp

    HECTF 我真是又菜又没时间肝题..又又又只水了波简单题... Reverse 1.Hello_Re file查一波 32bit,拖进IDA中 老规矩shift+F12 查看字符串: 跳转 F5查看 ...

  4. 凭借着这份面经,我拿下了字节,美团的offer!

    最近经常有粉丝私信问我问了一些诸如秋招该怎么复习的问题,我就想顺便把回答整理发一发.我也是把之前面试的一些经历经验和身边的人面试的经验总结了一下放在下面. 前期准备规划: 如果秋招的话一般过年回来就可 ...

  5. 思维导图iMindMap可以在哪些领域应用

    生活工作中你常常会遇到许多力所不能及的事情,感到无奈.茫然,这时候你急需一个帮手来帮你打破困境,思维导图就是这样的救世主,至于它有哪些力所能及的事情就是下面小编要跟你讲的. 你是否经常遇到过这样的情况 ...

  6. 用Camtasia来快速给录制的视频添加水印

    在日常生活中,视频的流行度越来越高,各种短视频的软件蜂拥上市,所以越来越多的人走上了自媒体的道路,在这条路上,谁的视频更加的精致,谁才能获得更多的关注度,相应的也能增加自己的人气. 但是在制作视频的过 ...

  7. guitar pro 系列教程(六):Guitar Pro音频导出功能之RSE音源

    让我们继续进行guitar pro的教程 上一章节,我们讲解了guitar Pro的播放与显示功能,在Guita pro的音源选择中分为两类,一种是自带的RES高保真音源,一种是MIDI输入音源.如果 ...

  8. 【VUE】2.渲染组件&重定向路由

    1.删除多余组件,使环境赶紧 1. 整理App.vue, 删除多余内容,在template 模板区域增加一个路由占位符 router-view:渲染路径匹配到的视图组件 <template> ...

  9. {"non_field_errors":["Unable to log in with provided credentials."]}% 无法使用提供的凭据登录

    在使用rest_framework_jwt进行登陆验证获取token的时候会报 {"non_field_errors":["Unable to log in with p ...

  10. Meetings S 题解

    题目描述 题目链接 有两个牛棚位于一维数轴上的点 \(0\) 和 \(L\) 处.同时有 \(N\) 头奶牛位于数轴上不同的位置(将牛棚和奶牛看作点).每头奶牛 \(i\) 初始时位于某个位置 \(x ...