比赛 |
20121106 |
评测结果 |
WAWAAAAAWAWWWAW |
题目名称 |
二十一点 |
最终得分 |
53 |
用户昵称 |
CAX_CPG |
运行时间 |
0.005 s |
代码语言 |
Pascal |
内存使用 |
0.31 MiB |
提交时间 |
2012-11-06 11:34:07 |
显示代码纯文本
uses math;
var f:array[1..1010]of longint;
a:array[1..1000]of longint;
i,n:longint;
function judge(x,y,z:longint):boolean;
begin
if x>=y then exit(false);
while z<=n do
begin
if x>16 then if x>=y then exit(false)else exit(true);
x:=x+a[z];if x>21 then exit(true);inc(z);
end;
end;
function suan(x,y:longint):longint;
begin while (x<16)and(y<n) do begin x:=x+a[y];inc(y);end;exit(y);end;
function g(x:longint):longint;
var turn,now,xx,c1,c2:longint;
begin
xx:=x;
if f[x]<>-1 then exit(f[x]);
if n-x+1<6 then begin f[x]:=0;exit(0);end;
turn:=1;c1:=a[x]+a[x+2];c2:=a[x+1]+a[x+3];inc(x,4);
while x<=n do
begin
now:=a[x];
case turn of
1:begin
if c1<=16 then
begin
inc(c1,now);turn:=1-turn;
if c1>21 then begin f[xx]:=max(f[xx],g(x+1)+1);break;end;
inc(x);continue;
end;
turn:=1-turn;
end;
0:begin
if judge(c1,c2,x)then
f[xx]:=max(f[xx],g(suan(c1,x))+1)
else f[xx]:=max(f[xx],g(suan(c1,x)));
c2:=c2+now;
if c2>21 then
begin f[xx]:=max(f[xx],g(x+1));break;end;
if (c1>16)and(c2>c1)then
begin f[xx]:=max(f[xx],g(x+1)+1);break;end;
if (x=n)and(c2>c1)then f[xx]:=max(f[xx],1);
turn:=1-turn;inc(x);
end;
end;
end;
exit(f[xx]);
end;
begin
assign(input,'jack.in');reset(input);
assign(output,'jack.out');rewrite(output);
readln(n);
for i:=1 to n do read(a[i]);
fillchar(f,sizeof(f),255);
writeln(g(1));
close(output);
end.