ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#204185 | #3596. 反转序列 | marcuse | 100 | 616ms | 167584kb | C++ | 1.2kb | 2024-05-02 10:10:58 | 2024-05-02 12:02:43 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
const int K = 200 + 10;
int n,k;
int T;
int id[K],minn[K],a[N];
int opt[N][K],w[N],b[N];
char ch;
int read(){
char ch; int sum = 0,flag = 1;
ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') flag = -flag; ch = getchar();}
while(ch >= '0' && ch <= '9'){sum = sum * 10 + ch - '0'; ch = getchar();}
return sum * flag;
}
int main(){
T = read();
while(T--){
n = read();
k = read();
memset(opt,0,sizeof(opt));
memset(w,0,sizeof(w));
memset(b,0,sizeof(b));
memset(minn,0,sizeof(minn));
memset(id,0,sizeof(id));
for(int i = 1; i <= n; i++){
cin >> ch;
a[i] = (ch - '0');
w[i] = w[i - 1] + (a[i] == 0);
b[i] = b[i - 1] + (a[i] == 1);
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= k; j++){
opt[i][j] = opt[i - 1][j] + a[i];
if(j > 0)
opt[i][j] = min(opt[i][j],opt[id[j - 1]][j - 1] + w[i] - w[id[j - 1]]);
if(opt[i][j] - w[i] < minn[j]){
minn[j] = opt[i][j] - w[i];
id[j] = i;
}
}
}
printf("%d\n",opt[n][k]);
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 24ms
memory: 167056kb
input:
2 4 2 1001 2 0 00
output:
0 0
result:
ok 2 lines
Test #2:
score: 10
Accepted
time: 8ms
memory: 167052kb
input:
1 6 1 100100
output:
1
result:
ok single line: '1'
Test #3:
score: 10
Accepted
time: 4ms
memory: 167056kb
input:
1 6 1 110101
output:
2
result:
ok single line: '2'
Test #4:
score: 10
Accepted
time: 27ms
memory: 167056kb
input:
2 1763 155 11110110011100100110000001100011101011101111011111111010110000111000010110001001111001110...
output:
299 0
result:
ok 2 lines
Test #5:
score: 10
Accepted
time: 20ms
memory: 167052kb
input:
1 2000 72 000000010000010100011000010001101000001000111000011000010110000000110100010010001000100100...
output:
264
result:
ok single line: '264'
Test #6:
score: 10
Accepted
time: 15ms
memory: 167052kb
input:
1 2000 68 111111010101111111101111110111101101101111100100111011011111111111010111111111100111101111...
output:
250
result:
ok single line: '250'
Test #7:
score: 10
Accepted
time: 90ms
memory: 167584kb
input:
1 200000 54 1000000000001011011000000000111010011011111110000010010000000010000000000111000001011001...
output:
59456
result:
ok single line: '59456'
Test #8:
score: 10
Accepted
time: 128ms
memory: 167584kb
input:
1 200000 114 111111000011100011001010000001100011010011011101100110010011100101001001000110000101001...
output:
88968
result:
ok single line: '88968'
Test #9:
score: 10
Accepted
time: 197ms
memory: 167584kb
input:
1 200000 200 110111010011010101011011110110111110111101111101101101111111111111111101101111011111111...
output:
43363
result:
ok single line: '43363'
Test #10:
score: 10
Accepted
time: 103ms
memory: 167584kb
input:
1 200000 79 1011111111111111111111110111111110111111111110111111111111111111111111111111111111111111...
output:
13780
result:
ok single line: '13780'
Extra Test:
score: 0
Extra Test Passed