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\) 条无向无权白边.你需要为每条白边指定边权 ...
随机推荐
- Java 单引号 与 双引号 区别
双引号,用来引用字符串, 单引号用来表示单个字符.
- 第10组 Beta冲刺 总结
1.基本情况 组长博客链接:https://www.cnblogs.com/cpandbb/p/14050808.html 答辩总结: ·因为alpha阶段的产品做得偏离了方向,所以beta冲刺大家非 ...
- 通过springBoot集成搭建webScoket服务器
前言: 最近工作中有一个需求,就是服务端要主动推送消息给客户端,而我们平常的Http请求只能一请求一响应,为此学习了webScokset通讯技术,以下介绍的是java 通过SpringBoot集成we ...
- Typora中本地图片无法上传CSDN解决方案
解决方法 本地图片无法上传,我们可以选择使用在线免费图床 把想要使用的图先上传到图床后复制对应的MarkDown语句到typora即可 在这里,给大家推荐一个图床 [图床链接](Image Uploa ...
- 【Java】System类时间戳
System类中获取时间戳 long time = System.currentTimeMillis(); //返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差称为时间戳 Syst ...
- http://dl-ssl.google.com/android上不去解决方案
转:https://blog.csdn.net/j04110414/article/details/44149653/ 一. 更新sdk,遇到了更新下载失败问题: Fetching https://d ...
- 【刷题-LeetCode】223. Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- golang中channel讲解
1. 无缓冲通道 2. 有缓冲通道 有缓冲通道特点:当channel已经满,在向里面写数据就会阻塞,当channel已经为空,在从里面读数据就会阻塞. 3. 关闭channel package mai ...
- Jquery Ajax添加header参数
在使用ajax请求接口时需要在请求头添加token来进行身份验证,方式如下: $.ajax({ type: 'GET', url: 'http://api.php', dataType: 'json' ...
- 【Vulnhub靶场】EMPIRE: BREAKOUT
环境准备 下载靶机,导入到vmware里面,这应该不用教了吧 开机可以看到,他已经给出了靶机的IP地址,就不用我们自己去探测了 攻击机IP地址为:192.168.2.15 靶机IP地址为:192.16 ...