BZOJ1032[JSOI2007] Ancestral Code Zuma

Source: Internet
Author: User

Portal description

This is a popular game in Jsoi, named Zuma. The delicate backdrop, complemented by mysterious Inca music, seems to be in the midst of an ancient country, playing a mysterious game-the famous Zuma game. Zuma game protagonist is a stone frog, the stone frog will spit out all kinds of color beads, beads shape beautiful, and has a mysterious color, surrounded by the stone frog is carrying beads of the track, all kinds of color beads will move forward along the track, the stone frog must stop the beads rolled into the orbital end of the hole inside, how to reduce beads it? You have to rely on the stone Frog Spit beads and tracks on the combination of beads, the color of the same person can disappear score! Until the beads on the track are cleared. Maybe you don't know the Zuma game. Never mind. Here we introduce a simple version of the Zuma game rules. There are some glass beads in one channel, each with its own color, as shown in 1. What the player can do is choose a color of the beads (note: The color can be selected, which is different from the real game) into a certain location.

Figure 1

In Figure 2, the player chooses a blue bead, which is placed in the diagram and gets a picture of 3.

Figure 2

Fig. 3 When the player enters a bead, the beads will disappear if they are made up of more than three consecutive beads of the same color as the beads. For example, a white bead is injected into the position in Figure 4, resulting in three white beads of the same color. These three beads will disappear, and then get the situation in Figure 5.

Figure 4

One thing to note in Figure 5 is that the three consecutive yellow beads in Figure 4 do not disappear because no beads are injected into them. The disappearance of beads will also have a ripple effect. When a string of beads of the same color disappears, if the beads around the vanishing position are the same color, and the length is greater than 2, you can continue to disappear. For example, in Figure 6, a red bead was injected, resulting in three consecutive red beads. When the red beads disappear, it is white beads around, and a total of four, so the white beads have disappeared. After that, the vanishing position of the left and right are blue beads, a total of three, so the blue beads also disappeared. Finally get the status of Figure 7. Note that the three yellow beads in Figure 7 do not disappear, because the blue beads disappear on one side of the purple beads, the other side is yellow beads, the color is different.

Figure 6

Figure 7 In addition to the above, there is no other way to eliminate the beads. Now, we have a row of beads that need you to eliminate. For each round, you are free to choose the beads of different colors and shoot them in any position. Your task is to shoot the fewest beads and eliminate all the beads.

Input

The first line is an integer n (n≤500), which represents the number of beads in the second row n integers (32-bit integers in the range), separated by spaces, each representing a color of beads.

Output

An integer that represents the minimum number of beads that need to be shot.

Sample Input9
1 1 2 2 3 3 2 1 1Sample Output1HINT

It is said that the problem marked the wrong way, resulting in a complete error of data ....

Exercises

This question is really crazy ... Think of a n^4 interval DP absolutely not. Write a n^2 the wrong solution is so right ...

We combine the same color and the adjacent balls together and record the number of overlaps. We remember F[I][J] to indicate the minimum number of I-to-j intervals to be deleted. Enumeration interval, midpoint, F[i][j]=min (F[i][j],f[i][k]+f[k+1][j]). We think about the same state of color. If the two sides are 1, then f[i][j]=min (f[i][j],f[i+1][j-1]+1), F[i][j]=min (F[i][j],f[i+1][j-1]). The final answer is F[1][n]. I think it is also necessary to consider two middle there is a same color ball and there are two non-adjacent single-color ball bar ...

Code
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespacestd;intN,cnt=0;structnode{intCol,num;} ba[510];intdp[510][510];intMain () {inti,j,k,x; scanf ("%d",&N); ba[0].col=-1;  for(i=1; i<=n;++i) {scanf ("%d",&x); if(ba[cnt].col!=x) ba[++cnt].col=x; Ba[cnt].num++; } memset (DP,0x7f,sizeof(DP));  for(i=1; i<=cnt;++i) {        if(ba[i].num==1) dp[i][i]=2; Elsedp[i][i]=1; }     for(j=2; j<=cnt;++j) {         for(i=1; i+j-1<=cnt;++i) {            if(ba[i].col==ba[i+j-1].col) {if(ba[i].num==1&& ba[i+j-1].num==1) {Dp[i][i+j-1]=min (dp[i][i+j-1],dp[i+1][i+j-2]+1); }                Elsedp[i][i+j-1]=min (dp[i][i+j-1],dp[i+1][i+j-2]); }             for(k=1; k<j;++k) {Dp[i][i+j-1]=min (dp[i][i+j-1],dp[i][i+k-1]+dp[i+k][i+j-1]); }}} printf ("%d\n", dp[1][cnt]);}

BZOJ1032[JSOI2007] Ancestral Code Zuma

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.