关于上次石子游戏题目100分解法(pascal)

来源:互联网 发布:linux重命名mv命令 编辑:程序博客网 时间:2024/05/17 16:16
var
        a,b:array[1..100000]of longint;
        s,i,j,n,ans:longint;
begin
        assign(input,'stone.in');reset(input);
        assign(output,'stone.out');rewrite(output);
        readln(n);
        for i:=1 to n do
        begin
                readln(a[i]);
                if a[i]=0 then inc(s);
        end;
        ans:=1;
        b[ans]:=1;
        for i:=2 to n do
        begin
                if (i mod 2=1)and(a[i]<>a[i-1]) then
                begin
                        inc(ans);
                        b[ans]:=i;
                end
                else if (i mod 2=0)and(a[i]<>a[i-1]) then
                begin
                        if a[i]=0 then s:=s+(i-b[ans])
                        else s:=s-(i-b[ans]);
                        if ans>1 then dec(ans);
                end;
        end;
        writeln(s);
        close(input);
        close(output);
end.

0 0
原创粉丝点击