bzoj1032: [JSOI2007]祖码Zuma

区间dp

先把连续相同的珠子压缩

1.可把[l,r]断成[l,i]U(i,r]

2.若两端点的值相同,考虑消去中间的珠子使两端点在一起

const
  maxn=500;
var
  m,n,i,a0,last:integer;
  a:array [1..maxn] of record
      data,num:integer;
    end;
  f:array [1..maxn,1..maxn] of integer;

function min(a,b:integer):integer;
begin
  if a>b then
    exit(b)
  else
    exit(a);
end;

function how(x:integer):integer;
begin
  if x<=2 then
    exit(3-x);
  exit(1);
end;

function dp(l,r:integer):integer;
var
  ans,i:integer;
begin
  if f[l,r]<>-1 then
    exit(f[l,r]);
  if l=r then
    exit(how(a[l].num));
  ans:=maxint;
  if a[l].data=a[r].data then
  begin
    if a[l].num+a[r].num>=3 then
      ans:=dp(l+1,r-1)
    else
      ans:=dp(l+1,r-1)+1;
  end;
  for i:=l to r-1 do
    ans:=min(ans,dp(l,i)+dp(i+1,r));
  f[l,r]:=ans;
  exit(ans);
end;

begin
  readln(n);
  m:=0;
  last:=-1;
  for i:=1 to n do
  begin
    read(a0);
    if a0<>last then
    begin
      inc(m);
      a[m].data:=a0;
      a[m].num:=1;
      last:=a0;
    end
    else
      inc(a[m].num);
  end;
  fillchar(f,sizeof(f),$ff);
  writeln(dp(1,m));
end.


阅读更多

Zuma

02-10

DescriptionnnnZuma is one of my favorite games of Fantasy Games series. In the game a lot of balls of different colors will enter the screen continuous, and the balls onscreen are rolling by a fixed track in a queue. You can control the stone frog located at the center of screen to fire balls. The balls fired can be inserted into the rolling balls queue. When there is a group of 3 or more balls of the same color touching, they explode! You should eliminate all the balls before they reach the skull which lies on the end of the track.nnLet’s consider a simplified game mode. There is a queue of balls of different colors given. At each time you can fire a ball of arbitrary color into the queue at arbitrary position (either between two balls or at one end of the queue). Arbitrary means that you can choose the color and the position on your behalf. When there is a group of M or more balls of the same color touching, they explode. Explosion causes the balls on either side to slide together, perhaps chain-reacting into another explosion.nnThere are some restrictions following:nnYou can only fire a ball of a certain color to a position where there is at least one ball of the same color touching. That is, after you fire the ball into the queue, there should be at least one ball of the same color touching the ball you fired.nThere may be some groups of M or more balls of the same color touching at the initial situation. These balls will explode just when another ball (not belonging to the group but has the same color) or another group of balls of the same color touches them.nWhat is number of firings do you need at least to finish the game?nnInputnnInput contains multiple test cases. Each test case starts with an integer M (2 ≤ M ≤ 20) in one line. Following is one line of nonempty capital letters (A to Z). Each character specifies one ball of the color represented by the letter. The length of each line is no more than 200.nnOutputnnThere is one line for each test case, which is the number of firings you need at least to finish the game.nnSample Inputnn3nAAABAAAn3nABBABBAnSample Outputnn2n2

没有更多推荐了,返回首页