算法:DP
挺简单的,但是一开始却想成了区间DP……
program vijos1331;
const
maxn=2500;
var
n,m:longint;
a:array [0..maxn] of char;
f:array [0..maxn] of longint;{f[i]表示前i个人最少分多少个巴士。}
procedure init;
var
i:longint;
begin
readln(n,m);
for i:=1 to n do readln(a[i]);
end;
procedure main;
var
i,j,th,tj:longint;
begin
fillchar(f,sizeof(f),$2F);
f[0]:=0;
f[1]:=1;
for i:=2 to n do
begin
if a[i]='H' then
begin
th:=1;
tj:=0;
end
else
begin
th:=0;
tj:=1;
end;
for j:=i-1 downto 1 do
begin
if a[j]='H' then inc(th) else inc(tj);
if (abs(th-tj)<=m) or (th=0) or (tj=0) then{如果满足条件的话,这一段就可以再分一组。}
begin
if f[j-1]+1<f[i] then
f[i]:=f[j-1]+1;
end;
end;
end;
end;
begin
assign(input,'VJ1331.in'); reset(input);
assign(output,'VJ1331.out'); rewrite(output);
init;
main;
writeln(f[n]);
close(input); close(output);
end.