vijos-p1323 2008.11.9
描述 Description
118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度的成品分开装箱,装箱员grant第1次顺序从流水线上取10个成品(如果一共不足10个,则全部取出),以后每一次把手中某种纯度的成品放进相应的箱子,然后再从流水线上顺序取一些成品,使手中保持10个成品(如果把剩下的全部取出不足10个,则全部取出),如果所有的成品都装进了箱子,那么grant的任务就完成了。
由于装箱是件非常累的事情,grant希望他能够以最少的装箱次数来完成他的任务,现在他请你编个程序帮助他。
输入格式 Input Format
第1行为n(1<=n<=100),为成品的数量
以后n行,每行为一个大写字母A,B或C,表示成品的纯度。
输出格式 Output Format
仅一行,为grant需要的最少的装箱次数。
样例输入 Sample Input
11
A
B
C
A
B
C
A
B
C
A
B
样例输出 Sample Output
3
f[t][s(i,t,'A')][k+s(i,t,'B')][x+s(i,t,'C')]=f[i][j][k][x]+1;
f[t][s(i,t,'A')+j][s(i,t,'B')][x+s(i,t,'C')]=f[i][j][k][x]+1;
f[t][s(i,t,'A')+j][s(i,t,'B')+k][s(i,t,'C')]=f[i][j][k][x]+1;
f[i][j][k][x] 意思是 操作序列最后一个物品为i个,有j个'A',k个'B',x个'C'时的最少次数..
第一排的t是 i+k;因为有可能>n 所有要特殊判断一下;意思是把'A'全部取完.
第2排的t是把'B'全部取完..代表的 i+j;
第3排的t是把'C'取完..表示i+x;
var
i,j,k,d,p,x,y,z,n,m,s:integer;
a:array[1..100] of char;
f:array[10..100,0..10,0..10,0..10] of integer;
begin
readln(n);
x:=0;y:=0;z:=0;
for i:=1 to n do
begin
readln(a[i]);
if i<=10 then
begin
if a[i]='A' then inc(x);
if a[i]='B' then inc(y);
if a[i]='C' then inc(z);
end;
end;
if n<=10 then
begin
s:=0;
if x>0 then inc(s);
if y>0 then inc(s);
if z>0 then inc(s);
writeln(s);
halt;
end;
for i:=10 to n do
for j:=0 to 10 do
for k:=0 to 10 do
for d:=0 to 10 do
f[i,j,k,d]:=-1;
f[10,x,y,z]:=0;
for i:=10 to n-1 do
for j:=0 to 10 do
for k:=0 to 10 do
for d:=0 to 10 do
if f[i,j,k,d]<>-1 then
begin
m:=i+j;
if m>n then m:=n;
x:=0;y:=k;z:=d;
for p:=i+1 to m do
begin
if a[p]='A' then inc(x);
if a[p]='B' then inc(y);
if a[p]='C' then inc(z);
end;
if f[m,x,y,z]=-1 then f[m,x,y,z]:=maxint;
if f[m,x,y,z]>f[i,j,k,d] then f[m,x,y,z]:=f[i,j,k,d]+1;
m:=i+k;
if m>n then m:=n;
x:=j;y:=0;z:=d;
for p:=i+1 to m do
begin
if a[p]='A' then inc(x);
if a[p]='B' then inc(y);
if a[p]='C' then inc(z);
end;
if f[m,x,y,z]=-1 then f[m,x,y,z]:=maxint;
if f[m,x,y,z]>f[i,j,k,d] then f[m,x,y,z]:=f[i,j,k,d]+1;
m:=i+d;
if m>n then m:=n;
x:=j;y:=k;z:=0;
for p:=i+1 to m do
begin
if a[p]='A' then inc(x);
if a[p]='B' then inc(y);
if a[p]='C' then inc(z);
end;
if f[m,x,y,z]=-1 then f[m,x,y,z]:=maxint;
if f[m,x,y,z]>f[i,j,k,d] then f[m,x,y,z]:=f[i,j,k,d]+1;
end;
s:=maxint;
for i:=0 to 10 do
for j:=0 to 10 do
for k:=0 to 10 do
if f[n,i,j,k]<>-1 then
begin
if i>0 then inc(f[n,i,j,k]);
if j>0 then inc(f[n,i,j,k]);
if k>0 then inc(f[n,i,j,k]);
if f[n,i,j,k]<s then s:=f[n,i,j,k];
end;
writeln(s);
end.