Đề bài: http://vn.spoj.com/problems/VPBINDEG/
Thuật toán:
- (đang cập nhập)
Code:
{$H+}
program Revolutions_VPbindeg;
const
fi = '';//Revolutions.inp';
fo = '';//Revolutions.out';
maxn = 10001;
var
n : longint;
s : string;
T,k : longint;
procedure open;
begin
assign(input,fi); reset(input);
assign(output,fo); rewrite(output);
end;
procedure clo;
begin
close(input);
close(output);
end;
function calc(k : longint) : longint;
var
i,s1,s0,kq : longint;
begin
s1:=0; s0:=0; kq:=0;
for i:=1 to n do if s[i]='1' then inc(s1);
for i:=1 to n do
if s[i]='0' then
begin
inc(s0);
if (s0>=k) and (s1>=k) then inc(kq,s1-k+1);
end
else dec(s1);
calc:=kq;
end;
procedure main;
var
test : longint;
begin
readln(T);
for test:=1 to T do
begin
readln(n,k);
readln(S);
writeln(calc(k)-calc(k+1));
end;
end;
BEGIN
open;
main;
clo;
END.
Speak Your Mind