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\) 条无向无权白边.你需要为每条白边指定边权 ...
随机推荐
- Kong 微服务网关在 Kubernetes 的实践
来源:分布式实验室译者:qianghaohao本文主要介绍将 Kong 微服务网关作为 Kubernetes (https://www.alauda.cn)集群统一入口的最佳实践,之前写过一篇文章使用 ...
- CMake与OpenMP
CMake与OpenMP cmake_minimum_required (VERSION 2.6) project (TEST) set (TEST_VERSION 0.1) set(CMAKE_BU ...
- .NET 云原生架构师训练营(KestrelServer源码分析)--学习笔记
目录 目标 源码 目标 理解 KestrelServer 如何接收网络请求,网络请求如何转换成 http request context(C# 可识别) 源码 https://github.com/d ...
- Java传递变量和对象的区别
传递对象 public class Demo03 { //引用传递:(实际上还是值传递)对于引用数据类型来说,传递的则是地址的副本(对象的地址).但由于地址副本和原来的类似,因此传递过去后形参也只想同 ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 44.Prim算法
public static void main(String[] args) { //测试看看图是否创建ok char[] data = new char[]{'A','B','C','D','E', ...
- 多线程-其他方法-join等
1 package multithread4; 2 3 /* 4 * toString():返回该线程的字符串表现形式,包括线程名称.优先级和线程组 5 * Thread[Thread-0,5,mai ...
- OrchardCore Headless建站
说到CMS系统,可能大家都能想起WordPress和Drupal之类的框架,作为.NET爱好者,一般也是知道一些基于.NET的CMS框架的,典型的比如DNN.Umbraco之类的.我很早之前听过Orc ...
- JavaCV的摄像头实战之四:抓图
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<JavaCV的摄像头实战> ...
- Linux下升级py2.6到2.7
python2.6版本有很多局限性:最重要的就是,当前最新的pip版本已经不再支持python2.6:这给我们安装很多python库增加了很多不便:所以这里把我升级python2.6--2.7的过程及 ...