给出一个跑得快一点的做法,洛谷最优解 (时间是第二名的 \(\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. linux 信号 ctrl + d z c fg bg 作用

    ctrl+c:前台进程终止 后台进程的终止: 方法一:通过jobs命令查看job号(假设为num),然后执行kill %num   $ kill %1 方法二:通过ps命令查看job的进程号(PID, ...

  2. 三、分布式编程总结------linux多线程服务端编程

  3. 如何实现Http请求报头的自动转发[应用篇]

    如今的应用部署逐渐向微服务化发展,导致一个完整的事务往往会跨越很多的应用或服务,出于分布式链路跟踪的需要,我们往往将从上游服务获得的跟踪请求报头无脑地向下游服务进行转发.本文介绍的这个名为Header ...

  4. Linear basis

    Linear basis is a relatively easy to learn but may not be useful algorithm. Below are two blogs that ...

  5. ABBYY FineReader 12/14版本功能对比及14产品优势

    FineReader 是一款一体化的 OCR 和PDF编辑转换器,随着版本的更新,功能的增加,FineReader 14的推出继续为用户在处理文档时提高业务生产力,该版本包含若干新特性和功能增强,包括 ...

  6. Echo Delay:FL中好用的声音制作处理方法

    今天来一起研究FL Studio的Echo Delay的作用,Echo Delay可以从MIDI输入创建回声,并允许我们通过音量,声像,切除和共振,音高和时间来操纵延迟. 图1:Echo Dealy ...

  7. Golang 实现 Redis(7): Redis 集群与一致性 Hash

    本文是使用 golang 实现 redis 系列的第七篇, 将介绍如何将单点的缓存服务器扩展为分布式缓存.godis 集群的源码在Github:Godis/cluster 单台服务器的CPU和内存等资 ...

  8. Vue看板娘教程1.0

    Live2D看板娘 前言(PS:本教程使用的Vue项目) 一.下载文件 二.使用步骤 1.引入文件 2.引入js 3.修改app.vue 4.如何换模型? 更换模型的效果 5.如何换语音? 结尾(后续 ...

  9. Docker安装基本命令操作,带你了解镜像和容器的概念!

    上一章节我们了解了Docker的基本概念,以及相关原理.这一章节进行实操. <Docker这么火爆.章节一:带你详尽了解Docker容器的介绍及使用> 一.Docker安装 声明:Dock ...

  10. exgcd 学习笔记

    最大公约数 更相减损术:\(\gcd(x,y)=\gcd(x,y-x)(x\leq y)\). 证明: 设 \(\gcd(x,y)=k\),则 \(x=kp,y=kq,\gcd(p,q)=1\). 那 ...