题目内容
塔子哥是一名信息学竞赛的热爱者,他从小就对编程和算法有着浓厚的兴趣。他经常参加各种信息学竞赛,从NOIP到NOI,再到IOI,他都有着不俗的成绩。他的梦想是成为一名优秀的程序员,为人类社会贡献自己的智慧。
有一天,塔子哥收到了一个来自国际信息学奥林匹克竞赛(IOI)组委会的邀请函,邀请他参加一个特别的挑战。这个挑战是由一位神秘的信息学大师设计的,只有通过了这个挑战,才能得到大师的认可和指导。塔子哥对此感到非常兴奋,他立刻打开了邀请函中附带的链接,进入了一个在线评测系统。
在评测系统中,塔子哥看到了这样一个题面:
给定一个长度为
n
n
n 的 01
串,你需要将其中的一些数字进行修改,使得所有连续的 0
的长度都是
a
a
a 的倍数,而所有连续的 1
的长度都是
b
b
b 的倍数。
为了解决这个问题,你可以对串中的某些位置进行修改,将 0
变为 1
或者将 1
变为 0
。但是你希望尽可能地少进行修改操作。
现在,请你告诉我你最少需要进行多少次操作才能达到上述要求。
输入描述
第一行输入三个正整数 n n n , a a a , b b b 。
第二行输入一个长度为
n
n
n 的仅包含字符 0
和 1
的字符串。
1 ≤ n , a , b ≤ 200000 1\le n,a,b \le 200000 1≤n,a,b≤200000
输出描述
使得字符串合法的最小操作次数,特别的,如果无论如何字符串都不能合法,请输出 − 1 -1 −1 。
样例1
输入
5 2 1
01011
输出
2
样例2
输入
8 4 2
01010001
输出
3
样例3
输入
8 3 3
01010001
输出
-1
思路:动态规划 + 前缀最值优化
Step1:朴素dp
考虑动态规划,做过Leetcode-132 或者 CodeFun2000-P1168 这种题的话,就会很自然的想到一个 O ( n 2 ) O(n^2) O(n2)的朴素 d p dp dp:
状态:
d p i , j ∈ { 0 , 1 } dp_{i,j \in \{0,1\}} dpi,j∈{0,1} 代表考虑了字符串的前 i i i个位置并且最后一段是 j j j 的最小修改次数。
转移:
考虑最后这个连续0/1段在哪个地方停止,转移方程如下:
d
p
i
,
0
=
m
i
n
j
<
i
∧
(
j
+
1
)
≡
i
(
m
o
d
a
)
{
m
i
n
(
d
p
j
,
0
,
d
p
j
,
1
)
+
C
o
u
n
t
j
+
1
,
i
,
1
}
dp_{i,0} = \underset{j < i \wedge (j+1) \equiv i(mod\ a)}{min}\{min(dp_{j,0},dp_{j,1}) + Count_{j+1,i,1}\} \\
dpi,0=j<i∧(j+1)≡i(mod a)min{min(dpj,0,dpj,1)+Countj+1,i,1}
其中
C
o
u
n
t
l
,
r
,
1
Count_{l,r,1}
Countl,r,1 代表区间
[
l
,
r
]
[l,r]
[l,r] 中
1
1
1 的个数。
d
p
i
,
1
dp_{i,1}
dpi,1同理。 复杂度为
O
(
n
3
)
O(n^3)
O(n3) , 前缀和优化成
O
(
n
2
)
O(n^2)
O(n2)
Step2:前缀最值优化
让我们拆一下
C
o
u
n
t
l
,
r
Count_{l,r}
Countl,r 成差分形式,把常量提出来得到:
$$
dp_{i,0} = \underset{j < i \wedge (j+1) \equiv i(mod\ a)}{min}{min(dp_{j,0},dp_{j,1}) + perOne_{i}-perOne_{j}} \ \
= perOne_{i}+\underset{j < i \wedge (j+1) \equiv i(mod\ a)}{min}{min(dp_{j,0},dp_{j,1}) -perOne_{j}}
KaTeX parse error: Can't use function '$' in math mode at position 50: …形象化的来讲,就是开a个桶,第$̲k$个桶维护$i, s.t.i…
mpA_i = \underset{j < i \wedge (j+1) \equiv i(mod\ a)}{min}{min(dp_{j,0},dp_{j,1}) -perOne_{j}} \
= min(mpA_{i-a} , min(dp_{j,0},dp_{j,1}) -perOne_{j}) \ \ \ \ \ \ -转移A
$$
那么
d
p
i
,
0
=
p
e
r
O
n
e
i
+
m
p
A
i
−
转移
B
dp_{i,0} = perOne_{i}+mpA_i \ \ \ \ \ \ -转移B
dpi,0=perOnei+mpAi −转移B
对于 d p i , 1 dp_{i,1} dpi,1 同理可得。
复杂度:
O ( n ) O(n) O(n)
实现细节见代码
代码
python
inf = 10**9
n , a , b = list(map(int , input().split()))
s = input()
# cnt[i][0] / cnt[i][1] 代表前缀[1,i]中 0/1的个数
cnt = [[0 , 0]]
for x in s:
cnt.append(cnt[-1][:])
v = ord(x) - ord('0')
cnt[-1][v] += 1
dp = [[inf , inf] for i in range(n + 1)]
mp_a = [inf for _ in range(a)]
mp_b = [inf for _ in range(b)]
mp_a[0] = 0 ,
mp_b[0] = 0
dp[0][0] = 0
dp[0][1] = 0
for i in range(1 , n + 1):
# 见题解中的转移B
if mp_a[i % a] != inf:
dp[i][0] = cnt[i][1] + mp_a[i % a]
if mp_b[i % b] != inf:
dp[i][1] = cnt[i][0] + mp_b[i % b]
# 见题解中的转移A
if min(dp[i]) != inf:
mp_a[i % a] = min(mp_a[i % a] , min(dp[i]) - cnt[i][1])
mp_b[i % b] = min(mp_b[i % b] , min(dp[i]) - cnt[i][0])
if min(dp[n]) == inf:
print(-1)
else:
print(min(dp[n]))
C++ (from 2333)
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define out(x) cout << #x << '=' << x << endl
#define out2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y << endl
#define no cout << "No" << endl; return
#define yes cout << "Yes" << endl; return
#define outvec(a) for (auto &v : a) { cout << v << ' '; } cout << endl
#define lowbit(x) (x & -x)
#define gcd __gcd
#define inf 0x3f3f3f3f3f3f3f3fLL
#define infi 0x3f3f3f3f
using ll = long long;
using pii = pair<int, int>;
template<typename T> ostream & operator << (ostream &out,const set<T>&obj){out<<"set(";for(auto it=obj.begin();it!=obj.end();it++) out<<(it==obj.begin()?"":", ")<<*it;out<<")";return out;}
template<typename T1,typename T2> ostream & operator << (ostream &out,const map<T1,T2>&obj){out<<"map(";for(auto it=obj.begin();it!=obj.end();it++) out<<(it==obj.begin()?"":", ")<<it->first<<": "<<it->second;out<<")";return out;}
template<typename T1,typename T2> ostream & operator << (ostream &out,const pair<T1,T2>&obj){out<<"<"<<obj.first<<", "<<obj.second<<">";return out;}
template<typename T> ostream & operator << (ostream &out,const vector<T>&obj){out<<"vector(";for(auto it=obj.begin();it!=obj.end();it++) out<<(it==obj.begin()?"":", ")<<*it;out<<")";return out;}
void solve() {
int n, a, b;
cin >> n >> a >> b;
string s;
cin >> s;
s = " " + s;
vector<int> cnt0(n + 1), cnt1(n + 1);
for (int i = 1; i <= n; i++) {
cnt0[i] = cnt0[i - 1];
cnt1[i] = cnt1[i - 1];
if (s[i] == '0') {
cnt0[i]++;
} else {
cnt1[i]++;
}
}
vector<int> preA(a + 1, infi), preB(b + 1, infi);
preA[0] = preB[0] = 0;
for (int i = 1; i <= n; i++) {
int costA = preA[i % a] + cnt1[i];
int costB = preB[i % b] + cnt0[i];
preA[i % a] = min(preA[i % a], costB - cnt1[i]);
preB[i % b] = min(preB[i % b], costA - cnt0[i]);
if (i == n) {
int ans = min(costA, costB);
if (ans >= infi) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}