给出一个跑得快一点的做法,洛谷最优解 (时间是第二名的 \(\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. Serilog 源码解析——数据的保存(中)

    上一篇文章中揭露了日志数据的绑定逻辑,主要说明了日志数据绑定的结果信息,即EventProperty结构体和LogEventProperty类,以及日志数据与具名属性Token的绑定类Property ...

  2. impala语句

    0.保留两位小数 round(字段a, 需要保留几位小数) round( data, 4) 1. case wen case when 字段a = '01' and 字段b = '01' and 字段 ...

  3. [原题复现]BJDCTF2020 WEB部分全部解

    简介  原题复现:https://gitee.com/xiaohua1998/BJDCTF2020_January  线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学 ...

  4. ubuntu16.04搭建LAMP(独立安装)

    修改APT源 备份原文件source.list sudo cp /etc/source.list /etc/source.list.bak 修改source.list sudo vi /etc/sou ...

  5. php 抛出异常

    <?php //try里面执行的东西如果不成立,可直接 throw new Exception('异常信息'),那么try里面的程序将会被停止执行,直接执行catch里面的程序 try { if ...

  6. python-基础入门-1

    Python的打印为   print,等价于c语言的printf 1 print "hello again" 就能打印出hello again,简简单单,就这么一句. 我用的vsc ...

  7. PowerPoint无法正常加载MathType的解决方法

    MathType是一款十分便捷的数学公式编辑器,可以和很多办公软件和网站兼容使用,我们日常用的比较多的也就是Office和WPS,更具体的说是Word\Excel\PPT等等一系列办公常用软件. 不过 ...

  8. 通俗解析莱文斯坦距离(Levenshtein Distance)计算原理(最小编辑距离)

    [版权声明]:本文章由danvid发布于http://danvid.cnblogs.com/,如需转载或部分使用请注明出处 最近看到一些动态规划的东西讲到莱文斯坦距离(编辑距离)的计算,发现很多都讲的 ...

  9. lca(lowestCommonAncestor)

  10. Eclipse导入包

    没有包,会报错: 鼠标放上去会有提示: 如果提示消失,可以使用快捷键:Ctrl+1调出提示信息. List的包有两个,集合里的List使用util包.