UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204177#3596. 反转序列drdilyor100578ms162500kbC++11906b2024-05-02 09:30:542024-05-02 12:01:40

answer

#include<bits/stdc++.h>
using namespace std;
int n,k;
string a;
int pre[200005];
int dp[200005][205];
int o[205];
void solve(){
	cin>>n>>k;
	cin>>a;
	a=" "+a;
	for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(a[i]=='1');
	for(int i=0;i<=n;i++){
		for(int j=0;j<=k;j++)dp[i][j]=(1<<30);
	}
	for(int i=0;i<=k;i++){
		o[i]=(1<<30);
	}
	o[0]=0;
	dp[0][0]=0;
	for(int i=1;i<=n;i++){
		for(int j=0;j<=k;j++)dp[i][j]=dp[i-1][j]+(a[i]-'0');
		for(int j=1;j<=k;j++){
			dp[i][j]=min(dp[i][j],i-pre[i]+o[j-1]);
			/*
			for(int x=0;x<i;x++){
				dp[i][j]=min(dp[i][j],(i-pre[i])+dp[x][j-1]-x+pre[x]);
			}*/
		}
		if(i==n)continue;
		for(int j=0;j<=k;j++){
			o[j]=min(o[j],dp[i][j]-i+pre[i]);
		}
	}
	int res=1<<30;
	for(int i=0;i<=k;i++)res=min(res,dp[n][i]);
	cout<<res<<"\n";
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0);
	int t;
	cin>>t;
	while(t--)solve();
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1276kb

input:

2
4 2
1001
2 0
00

output:

0
0

result:

ok 2 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 1280kb

input:

1
6 1
100100

output:

1

result:

ok single line: '1'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1276kb

input:

1
6 1
110101

output:

2

result:

ok single line: '2'

Test #4:

score: 10
Accepted
time: 4ms
memory: 2696kb

input:

2
1763 155
11110110011100100110000001100011101011101111011111111010110000111000010110001001111001110...

output:

299
0

result:

ok 2 lines

Test #5:

score: 10
Accepted
time: 0ms
memory: 2888kb

input:

1
2000 72
000000010000010100011000010001101000001000111000011000010110000000110100010010001000100100...

output:

264

result:

ok single line: '264'

Test #6:

score: 10
Accepted
time: 0ms
memory: 2884kb

input:

1
2000 68
111111010101111111101111110111101101101111100100111011011111111111010111111111100111101111...

output:

250

result:

ok single line: '250'

Test #7:

score: 10
Accepted
time: 87ms
memory: 162496kb

input:

1
200000 54
1000000000001011011000000000111010011011111110000010010000000010000000000111000001011001...

output:

59456

result:

ok single line: '59456'

Test #8:

score: 10
Accepted
time: 129ms
memory: 162500kb

input:

1
200000 114
111111000011100011001010000001100011010011011101100110010011100101001001000110000101001...

output:

88968

result:

ok single line: '88968'

Test #9:

score: 10
Accepted
time: 240ms
memory: 162500kb

input:

1
200000 200
110111010011010101011011110110111110111101111101101101111111111111111101101111011111111...

output:

43363

result:

ok single line: '43363'

Test #10:

score: 10
Accepted
time: 118ms
memory: 162500kb

input:

1
200000 79
1011111111111111111111110111111110111111111110111111111111111111111111111111111111111111...

output:

13780

result:

ok single line: '13780'

Extra Test:

score: 0
Extra Test Passed