zoukankan      html  css  js  c++  java
  • Halloween Costumes

    Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Halloween, these parties are all costume parties, Gappu always selects his costumes in such a way that it blends with his friends, that is, when he is attending the party, arranged by his comic-book-fan friends, he will go with the costume of Superman, but when the party is arranged contest-buddies, he would go with the costume of 'Chinese Postman'.

    Since he is going to attend a number of parties on the Halloween night, and wear costumes accordingly, he will be changing his costumes a number of times. So, to make things a little easier, he may put on costumes one over another (that is he may wear the uniform for the postman, over the superman costume). Before each party he can take off some of the costumes, or wear a new one. That is, if he is wearing the Postman uniform over the Superman costume, and wants to go to a party in Superman costume, he can take off the Postman uniform, or he can wear a new Superman uniform. But, keep in mind that, Gappu doesn't like to wear dresses without cleaning them first, so, after taking off the Postman uniform, he cannot use that again in the Halloween night, if he needs the Postman costume again, he will have to use a new one. He can take off any number of costumes, and if he takes off k of the costumes, that will be the last k ones (e.g. if he wears costume Abefore costume B, to take off A, first he has to remove B).

    Given the parties and the costumes, find the minimum number of costumes Gappu will need in the Halloween night.

    Input

    Input starts with an integer T (≤ 200), denoting the number of test cases.

    Each case starts with a line containing an integer N (1 ≤ N ≤ 100)denoting the number of parties. Next line contains N integers, where the ith integer ci (1 ≤ ci ≤ 100) denotes the costume he will be wearing in party i. He will attend party 1 first, then party 2, and so on.

    Output

    For each case, print the case number and the minimum number of required costumes.

    Sample Input

    2

    4

    1 2 1 2

    7

    1 2 1 1 3 2 1

    Sample Output

    Case 1: 3

    Case 2: 4

    题目意思:按顺序去参加舞会。每个舞会对衣服都有要求。可以连续穿好多件衣服。需要时候就脱下来,但是一旦脱下来,这件衣服就报废了。问最少需要几件衣服。

    解题思路:状态转移方程: dp[i][j] = max(dp[i][j],dp[i+1][k]+dp[k+1][j]);

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include<math.h>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int MAX = 200;
     9 
    10  int a[MAX],M[MAX][MAX];
    11 
    12 int main()
    13 {
    14     int N;
    15     cin>>N;
    16     int N0=0;
    17     while(N--)
    18     {
    19         int n;
    20         cin>>n;
    21         for(int i=1;i<=n;i++)
    22             cin>>a[i];
    23         memset(M,0,sizeof(M));
    24         for(int i=1;i<=n;i++)
    25             for(int j=i;j<=n;j++)
    26                  M[i][j]=j-i+1;
    27 
    28 
    29         for(int i =n-1;i>=1;i--)
    30             for(int j = i+1;j<=n;j++)
    31             {
    32                 M[i][j] = M[i+1][j]+1;
    33                 for(int k =i;k<=j;k++)
    34                 {
    35                     if(a[k]==a[i])
    36                     {
    37                         M[i][j] = min(M[i][j],M[i+1][k-1]+M[k][j]);
    38                     }
    39                 }
    40             }
    41         cout<<"Case "<<++N0<<": "<<M[1][n]<<endl;
    42 
    43     }
    44 
    45     return 0;
    46 }
  • 相关阅读:
    可展开的UITableView (附源码)
    如何给Wordpress安装插件
    使用SAP UI5 Web Components开发React应用
    如何在SAP Fiori应用里使用React component
    SAP云平台上的Fiori Launchpad tile数据是如何从后台取出来的
    ABAP Netweaver和SAP Hybris的内存管理
    SAP CRM的Genil层和Hybris的jalo模型
    wordpress插件在服务器上的存储位置
    一步步把一个SpringBoot应用打包成Docker镜像并运行
    SAP物料主数据创建时间和创建个数的函数关系
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7375814.html
Copyright © 2011-2022 走看看