Light OJ 1422 Halloween Costumes interval DP basic problem

Source: Internet
Author: User

Halloween Costumes Topic Links:

http://lightoj.com/volume_showproblem.php?problem=1422

Test instructions

Gappu want to go to some party, he went to each one to put a specific number of clothes on the outside, he can put on or take off the clothing (take off the clothing can not be worn again, but may wear a new dress of the same number, the recent clothing will be put on the outside of the clothing before), Ask Gappu at least how many sets of clothing to prepare.

Exercises

Set DP[I][J] for the interval I to J (set Len to interval length, j=i+len) The minimum number of clothing required, then you can find Dp[i][j] can be obtained in two ways:

①dp[i][j]=dp[i][j-1]+1

② find a point k,k in the interval [i][j-1] and party K needs costumes and party J needs the same costumes, then Dp[i][j]=dp[i][k]+dp[k+1][j-1] (k+1 is to ensure that the party K required to take off the garment)

Take ① and ② smaller values, dp[1][n] as the end state

Code

#include <stdio.h>
const int n=101;
int p[n],dp[n][n];
int mmin (int x,int y)
{
Return x<y?x:y;
}
void Solve ()
{
int t,n,case=0;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
for (int i=1;i<=n;++i)
{
scanf ("%d", &p[i]);
Dp[i][i]=1;
}
for (int len=1;len<n;++len)
for (int i=1;i+len<=n;++i)
{
int J=i+len;
dp[i][j]=dp[i][j-1]+1;
for (int k=i;k<=j-1;++k)
if (P[k]==p[j])
{
Dp[i][j]=mmin (Dp[i][j],dp[i][k]+dp[k+1][j-1]);
}
}
printf ("Case%d:%d\n", ++case,dp[1][n]);
}
}
int main ()
{
Solve ();
return 0;
}

Light OJ 1422 Halloween Costumes interval DP basic problem

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.