题解 CF1446D2 【Frequency Problem (Hard Version)】
给出一个跑得快一点的做法,洛谷最优解 (时间是第二名的 \(\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)】的更多相关文章
- Codeforces 1446D2 - Frequency Problem (Hard Version)(根分)
Codeforces 题面传送门 & 洛谷题面传送门 人菜结论题做不动/kk 首先考虑此题一个非常关键的结论:我们设整个数列的众数为 \(G\),那么在最优子段中,\(G\) 一定是该子段的众 ...
- 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 ...
- 【题解】Tree-String Problem Codeforces 291E AC自动机
Prelude 传送到Codeforces:(/ω\)--- (/ω•\) Solution 很水的一道题. 对查询的串建出来AC自动机,然后树上随便跑跑就行了. 为什么要写这篇题解呢? 我第一眼看到 ...
- 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. ...
- P1832题解 A+B Problem(再升级)
万能的打表 既然说到素数,必须先打素数表筛出素数, 每个素数可以无限取,这就是完全背包了. 这次打个质数表: bool b[1001]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1 ...
- 题解 CF1428G Lucky Numbers (Easy Version and Hard Version)
这题没有压行就成 \(\texttt{Hard Version}\) 最短代码解了( 要知道这题那么 \(sb\) 就不啃 \(D\) 和 \(E\) 了. \(\texttt{Solution}\) ...
- 题解:T103342 Problem A. 最近公共祖先
题目链接 题目大意 求每个点对的lca深度的和 以每一层分析,得出通式 由于1e9的数据范围要化简表达式得到O(能过) 瞎搞后就是2^(2n+2)-(4n+2)*2^n-2 code: #includ ...
- 多校联训 DS 专题
CF1039D You Are Given a Tree 容易发现,当 \(k\) 不断增大时,答案不断减小,且 \(k\) 的答案不超过 \(\lfloor\frac {n}{k}\rfloor\) ...
- 记一次jdk升级引起的 Unsupported major.minor version 51.0
之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...
随机推荐
- 优测 x QTA 兼容性测试全面启动啦
鉴于Android系统开放性开发,以及Android系统在智能机市场的覆盖率超高的特点,APP的兼容性测试需求大幅提升.为迎合市场需求,优测(Utest)与QTA合作,全力打造兼容性测试服务.由优测提 ...
- innodb为什么需要doublewrite(转)
InnoDB的page size默认是16KB,而操作系统的一个block size是4KB,磁盘io block则更小.那么InnoDB的page刷到磁盘上要写4个操作系统block,在极端情况下( ...
- Python_scrapyRedis零散
1. # Redis 1.解压,配环境变量 2.win上设置自启动 redis-server --service-install D:\redis\redis.windows.conf --logle ...
- Redis订阅
1.Redis订阅简介 进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 2.Redis订阅命令 3.Redis订阅的使用 先订阅后发布后才能收到消息, 1 可以一次性订 ...
- 2、Spring Boot配置
1.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...
- wget 快速下载 ftp 文件
GNU Wget 1.17.1,非交互式的网络文件下载工具. 用法: wget [选项]... [URL]... 长选项所必须的参数在使用短选项时也是必须的. 启动: -V, --version 显示 ...
- bWAPP----HTML Injection - Reflected (GET)
HTML Injection - Reflected (GET) 进入界面, html标签注入 这是核心代码 1 <div id="main"> 2 3 <h1& ...
- Hadoop大数据平台之Zookeeper搭建
环境:CentOS 7.4 (1708 DVD) 工具:MobaXterm 1. 使用xftp将hadoop上传到/usr/local目录下,将其解压并重命名. 2. 修改环境变量并source. ...
- Java8常用的内置函数式接口(一)Predicate、Consumer、Supplier、Function
Java8常用的内置函数式接口(一) 简介 JDK 1.8 API中包含了很多内置的函数式接口.有些是在以前版本的Java中大家耳熟能详的,例如Comparator接口,或者Runnable接口.对这 ...
- Javascript 严格模式的一些理解
平常我们在ECMAscript5中可以声明以下的变量 num = 5; console.log(num);//输出的是变量5 而为了Javascript更合理.更安全.更严谨的方向发展,添加了一种新的 ...