UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204210#3596. 反转序列smallstone100476ms4332kbC++111.1kb2024-05-02 11:43:332024-05-02 12:07:56

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//using LL = __int128
ll n, k, a[222222], sum[222222], t, dp[2][222], g[222], h[222];
void debug(int x){
	cout << x << ": ";
	for(int i = 0 ; i <= k ; i++)
		cout << dp[x & 1][i] << " \n"[i == k];
}
int main(){
	scanf("%lld", &t);
	while(t--){
		scanf("%lld%lld", &n, &k);
		for(int i = 1 ; i <= n ; i++){
		scanf("%1lld", &a[i]);
			sum[i] = a[i] + sum[i - 1];
		}
		memset(g, 0x3f, sizeof g);
		memset(h, 0x3f, sizeof h);
		g[0] = h[0] = 0;
		for(ll i = 1 ; i <= n ; i++){
			dp[i & 1][0] = sum[i];
			g[0] = min(g[0], dp[i & 1][0] + sum[i] - i);
			h[0] = min(h[0], dp[i & 1][0] - sum[i]);
			for(int j = 1 ; j <= min(k, i) ; j++){
				//dp[i & 1][j] = LONG_LONG_MAX;
				dp[i & 1][j] = min(h[j] + sum[i], g[j - 1] - sum[i] + i);
				g[j] = min(g[j], dp[i & 1][j] + sum[i] - i);
				h[j] = min(h[j], dp[i & 1][j] - sum[i]);
			}
			//debug(i);
		}
		ll ans = LONG_LONG_MAX;
		for(int i = 0 ; i <= k ; i++)
			ans = min(ans, dp[n & 1][i]);
		printf("%lld\n", ans);
	}
	return 0;
}

详细

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

Test #1:

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

input:

2
4 2
1001
2 0
00

output:

0
0

result:

ok 2 lines

Test #2:

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

input:

1
6 1
100100

output:

1

result:

ok single line: '1'

Test #3:

score: 10
Accepted
time: 1ms
memory: 1200kb

input:

1
6 1
110101

output:

2

result:

ok single line: '2'

Test #4:

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

input:

2
1763 155
11110110011100100110000001100011101011101111011111111010110000111000010110001001111001110...

output:

299
0

result:

ok 2 lines

Test #5:

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

input:

1
2000 72
000000010000010100011000010001101000001000111000011000010110000000110100010010001000100100...

output:

264

result:

ok single line: '264'

Test #6:

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

input:

1
2000 68
111111010101111111101111110111101101101111100100111011011111111111010111111111100111101111...

output:

250

result:

ok single line: '250'

Test #7:

score: 10
Accepted
time: 75ms
memory: 4332kb

input:

1
200000 54
1000000000001011011000000000111010011011111110000010010000000010000000000111000001011001...

output:

59456

result:

ok single line: '59456'

Test #8:

score: 10
Accepted
time: 117ms
memory: 4332kb

input:

1
200000 114
111111000011100011001010000001100011010011011101100110010011100101001001000110000101001...

output:

88968

result:

ok single line: '88968'

Test #9:

score: 10
Accepted
time: 197ms
memory: 4332kb

input:

1
200000 200
110111010011010101011011110110111110111101111101101101111111111111111101101111011111111...

output:

43363

result:

ok single line: '43363'

Test #10:

score: 10
Accepted
time: 86ms
memory: 4328kb

input:

1
200000 79
1011111111111111111111110111111110111111111110111111111111111111111111111111111111111111...

output:

13780

result:

ok single line: '13780'

Extra Test:

score: 0
Extra Test Passed