UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204188#3596. 反转序列wwwyyww1002811ms6188kbC++11924b2024-05-02 10:17:222024-05-02 12:03:06

answer

#include <bits/stdc++.h>
using namespace std;
int t,n,k,f[210][200010],sum[200010];
string s;

struct node{
	int x,num;
	node(int xx, int numm) {
		x=xx,num=numm;
	}
	bool operator < (const node &a) const{
		return num < a.num;
	}
};

int main(){
	cin >> t;
	while(t--) {
		cin >> n >> k >> s;
		
		while(k--) {
			int ans=0,l=0,r=0;
			priority_queue <node> q;
			q.push(node(-1,0));
			for(int i = 0; i < s.size(); i++) {
				sum[i] = sum[i-1]+(s[i]=='0'?1:-1);
				if(!q.empty()){
					if(sum[i]-q.top().num<ans){
						ans=sum[i]-q.top().num;
						l=q.top().x,r=i;
					}
				}
				q.push(node(i,sum[i]));
				//cout << q.top().num << " ";
			}
			for(int i = l+1; i <= r; i++) {
				if(s[i]=='0')s[i]='1';
				else s[i]='0';
			}
			//cout << s <<endl;
		}
		int ans=0;
		for(int i = 0; i < s.size(); i++) {
			ans+=(s[i]-'0');
		}
		cout << ans << endl;
	}
	return 0;
}

详细

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

Test #1:

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

input:

2
4 2
1001
2 0
00

output:

0
0

result:

ok 2 lines

Test #2:

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

input:

1
6 1
100100

output:

1

result:

ok single line: '1'

Test #3:

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

input:

1
6 1
110101

output:

2

result:

ok single line: '2'

Test #4:

score: 10
Accepted
time: 7ms
memory: 1288kb

input:

2
1763 155
11110110011100100110000001100011101011101111011111111010110000111000010110001001111001110...

output:

299
0

result:

ok 2 lines

Test #5:

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

input:

1
2000 72
000000010000010100011000010001101000001000111000011000010110000000110100010010001000100100...

output:

264

result:

ok single line: '264'

Test #6:

score: 10
Accepted
time: 2ms
memory: 1296kb

input:

1
2000 68
111111010101111111101111110111101101101111100100111011011111111111010111111111100111101111...

output:

250

result:

ok single line: '250'

Test #7:

score: 10
Accepted
time: 329ms
memory: 5724kb

input:

1
200000 54
1000000000001011011000000000111010011011111110000010010000000010000000000111000001011001...

output:

59456

result:

ok single line: '59456'

Test #8:

score: 10
Accepted
time: 594ms
memory: 5728kb

input:

1
200000 114
111111000011100011001010000001100011010011011101100110010011100101001001000110000101001...

output:

88968

result:

ok single line: '88968'

Test #9:

score: 10
Accepted
time: 1314ms
memory: 6188kb

input:

1
200000 200
110111010011010101011011110110111110111101111101101101111111111111111101101111011111111...

output:

43363

result:

ok single line: '43363'

Test #10:

score: 10
Accepted
time: 561ms
memory: 5724kb

input:

1
200000 79
1011111111111111111111110111111110111111111110111111111111111111111111111111111111111111...

output:

13780

result:

ok single line: '13780'

Extra Test:

score: 0
Extra Test Passed