ABC391E题解
大概评级:绿。
题目传送门。
显然动态规划,设 \(f_{i,k}\) 表示经过 \(i\) 次变换后能将 \(a_k\) 取反的最大值,显然答案为 \(f_{n,1}\),状态转移很简单,枚举 \(i\) 和 \(k\),我们肯定是从 \(f_{i-1,x}\) 转移过来的,\(x\) 可以取什么值呢,想想合并的过程,从 \(i-1\) 次变换到第 \(i\) 次变换,区别在于将 \(i-1\) 次变换后的 \(a_{3k-2},a_{3k-1},a_{3k}\) 再次合并,得到第 \(i\) 次变换的结果,所以 \(x\) 的取值只能是 \(3k-2,3k-1,3k\),当然,这里不需要枚举 \(x\),我们只需要看第 \(i-1\) 次变换后的这三个位置,进行分类讨论即可。设 \(one = 3k-2,two = 3k-1,three = 3k\),注意,我们每次遍历 \(i\) 时,都得准备一个数组 \(A\) 表示第 \(i-1\) 次变换后的 \(a\),则状态转移为:如果 \(A_{one} = A_{two}\) 并且 \(A_{two} = A_{three}\),说明此时三个数相等,那么 \(f_{i,k} = f_{i-1,one}+f_{i-1,two}+f_{i-1,three}-\max\left\{f_{i-1,one},f_{i-1,two},f_{i-1,three}\right\}\),其实就是当三个位置的数全部相等时,我们想要取反,那得取反这三个位置中的两个数才行,那肯定是取两个 \(f\) 值最小的改变合并后的结果,由于第二小不太好整,我们就拿总和减去 \(f\) 值最大的那个位置就可以了,第二种情况是如果 \(A_{one} = A_{two}\) 但是 \(A_{two} \not= A_{three}\),那么只需要找 \(two\) 或 \(one\) 这两个位置随便一个位置取反就行了,也就是 \(f_{i,k} = \min(f_{i-1,one},f_{i-1,two})\),然后第三种情况 \(A_{one} = A_{three}\) 但是 \(A_{three} \not= A_{two}\),跟上一种情况差不多,\(f_{i,k} = \min(f_{i-1,one},f_{i-1,three})\),最后一种情况也是一样的,就是如果 \(A_{two} = A_{three}\) 但是 \(A_{three} \not= A_{one}\),也是一样的,\(f_{i,k} = \min(f_{i-1,two},f_{i-1,three})\)。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1594328;
char a[N];
char b[N];
int f[18][N];
signed main()
{
int n;
scanf("%d",&n);
scanf("%s",a+1);
int m = pow(3,n);
for(int i = 1;i<=m;i++)
{
f[0][i] = 1;
}
int num = 1;
for(int i = 1;i<=n;i++)
{
num*=3;
int mm = m/num;
for(int j = 1;j<=mm;j++)
{
int one = 3*j-2,two = 3*j-1,three = 3*j;
if(a[one] == a[two]&&a[two] == a[three])
{
b[j] = a[one];
f[i][j] = f[i-1][one]+f[i-1][two]+f[i-1][three]-max(max(f[i-1][one],f[i-1][two]),f[i-1][three]);
}
else
{
if(a[one] == a[two])
{
b[j] = a[one];
f[i][j] = min(f[i-1][one],f[i-1][two]);
}
else if(a[one] == a[three])
{
b[j] = a[one];
f[i][j] = min(f[i-1][one],f[i-1][three]);
}
else
{
b[j] = a[two];
f[i][j] = min(f[i-1][two],f[i-1][three]);
}
}
}
for(int j = 1;j<=mm;j++)
{
a[j] = b[j];
}
}
printf("%d",f[n][1]);
return 0;
}

浙公网安备 33010602011771号