Solution -「CF 1392G」Omkar and Pies
\(\mathcal{Description}\)
Link.
给定两个长度为 \(K\) 的 \(01\) 串 \(S,T\) 和 \(n\) 组操作 \((a_i,b_i)\),意义为交换 \(S_{a_i}\) 和 \(S_{b_i}\)。你需要执行一段长度不小于 \(m\) 的连续操作区间,最大化 \(S\) 和 \(T\) 相同的位数。求出最大相同位数。
\(K\le20\),\(m\le n\le10^6\)。
\(\mathcal{Solution}\)
一个简单的性质:\(S\) 和 \(T\) 同时执行相同操作,答案不变。
那么可以想到利用后缀和——记 \(S_i\) 表示 \(S\) 依次执行操作 \(i\sim n\) 所得到的串,\(T_i\) 同理。根据性质,\(S\) 操作区间 \([l,r]\) 后与 \(T\) 的相同位数等于 \(S_l\) 和 \(T_{r+1}\) 的相同位数。
现在问题变成,给定两个长为 \(n+1\) 的串序列 \(\{S_{n+1}\},\{T_{n+1}\}\),求:
\]
其中 \(\operatorname{same}\) 表示两个串的相同位数。
接下来考虑 DP。定义:
f(1,v)=\max_{v\in T_i}i
\]
枚举二进制 \(v\),转移,如果有 \(f(0,v)+m-1\le f(1,v)\),就可以用 \(\operatorname{popcount}(v)\) 更新答案。
\(\mathcal{Code}\)
/* Clearink */
#include <cstdio>
inline int rint ( const int base = 10 ) {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * base + ( s ^ '0' );
return x;
}
template<typename Tp>
inline void wint ( Tp x ) {
if ( x < 0 ) putchar ( '-' ), x = ~ x + 1;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
inline void chkmin ( int& a, const int b ) { b < a ? a = b : 0; }
inline void chkmax ( int& a, const int b ) { a < b ? a = b : 0; }
const int MAXN = 1e6, MAXS = 1 << 20;
int n, m, S, T, K, a[MAXN + 5], b[MAXN + 5], p[MAXN + 5], f[2][MAXS | 5];
inline int swp ( int x ) {
int ret = 0;
for ( int i = 0; i < K; ++ i ) ret |= ( ( x >> i ) & 1 ) << p[i];
return ret;
}
int main () {
n = rint (), m = rint (), K = rint ();
S = rint ( 2 ), T = rint ( 2 );
for ( int i = 1; i <= n; ++ i ) a[i] = K - rint (), b[i] = K - rint ();
for ( int s = 0; s < 1 << K; ++ s ) f[0][s] = n + 1, f[1][s] = -1;
f[1][T] = n + 1;
for ( int i = 0; i < K; ++ i ) p[i] = i;
for ( int i = n; i; -- i ) {
p[a[i]] ^= p[b[i]] ^= p[a[i]] ^= p[b[i]];
chkmin ( f[0][swp ( S )], i ), chkmax ( f[1][swp ( T )], i );
}
int ans = -1, ansl = -1, ansr = -1;
for ( int s = ( 1 << K ) - 1; ~s; -- s ) {
for ( int i = 0; i < K; ++ i ) {
if ( !( ( s >> i ) & 1 ) ) continue;
chkmin ( f[0][s ^ ( 1 << i )], f[0][s] );
chkmax ( f[1][s ^ ( 1 << i )], f[1][s] );
}
if ( f[1][s] - f[0][s] >= m ) {
if ( int t = __builtin_popcount ( s ); ans < t ) {
ans = t, ansl = f[0][s], ansr = f[1][s] - 1;
}
}
}
ans = 2 * ans + K - __builtin_popcount ( S ) - __builtin_popcount ( T );
wint ( ans ), putchar ( '\n' );
wint ( ansl ), putchar ( ' ' ), wint ( ansr ), putchar ( '\n' );
return 0;
}
Solution -「CF 1392G」Omkar and Pies的更多相关文章
- Solution -「CF 1372E」Omkar and Last Floor
\(\mathcal{Description}\) Link. 给定一个 \(n \times m\) 的矩阵,每行被划分为若干段,你可以钦定每段中恰好一个位置为 \(1\),其余位置为 \( ...
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
随机推荐
- js 动态设置键值对数组 ,类似于 java 的Map 类型
1.前言 我想设置一个数据 var json = {a1 :1 , a2 :2 , a3 :3 .....} 这样的动态数据 ,怎么写呢? 2.正确写法 var json = []; for ...
- 安装TestNG
1.启动Eclipse,选择"Help"-->'Install New Software' 2.在谈出的弹框中选择"Add" 3.在弹出的"Ad ...
- [Windows]为windows系统鼠标右键添加软件和图标
转载自 https://blog.csdn.net/p312011150/article/details/81207059 一.打开注册表 首先打开windows的注册表,当然了,我个人倾向于 (1) ...
- 【笔记】HOG (Histogram of Oriented Gradients, 方向梯度直方图)的开源实现
wiki上的介绍 OpenCV的实现 cv::HOGDescriptor Struct Reference opencv cv::HOGDescriptor 的调用例子 HOGDescriptor h ...
- Servlet Filter(过滤器)
Servlet Filter 又称 Servlet 过滤器,它是在 Servlet 2.3 规范中定义的,能够对 Servlet 容器传给 Web 资源的 request 对象和 response 对 ...
- Rsync安装配置
一.先准备两台CentOS服务器,假定是 1.172.18.2.225(服务端) 需要配置rsyncd.conf文件 2.172.18.2.227(客户端) 不需要配置rsyncd.conf文件 二. ...
- linux下怎么挂载U盘
一般来说linux系统会自动挂载u盘,如果是图形界面,你一插上u盘会自动弹出来文件夹. 如果是命令行界面 使用命令 fdisk -l (root下执行) 显示如下 一般u盘都是fat32格式 ...
- Docker 与 K8S学习笔记(二十)—— 使用Downward API向容器注入Pod信息
Kubernetes在创建Pod时,会为Pod和容器设置一些额外的信息,比如Pod名称.Pod IP.Node IP.Label.Annotation.资源限制等,我们经常会在应用程序中使用到这些数据 ...
- linux文件编辑器快捷方式
一:文件编辑器快捷方式 7.光标快速移动快捷方式 ①. 快速切换光标到底行 shift + G ②. 快速切换光标到首行 gg ③. 快速跳转到行首 0 ④. 快速跳转到行尾 shift + $ ⑤. ...
- TF-IDF计算相似度为什么要对稀疏向量建立索引?
TF-IDF的向量表示的稀疏问题 之前在看tf-idf代码时候思考了一个问题,不知道对于初学的大部分同学有没有这样一个疑惑,用tf-idf值构成的向量,维度可能跟词表的大小有关,那么对于一句话来说,这 ...