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 }
  • 相关阅读:
    Perl 正则匹配经验记录
    Linux——高效玩转命令行
    推荐一个SAM文件或者bam文件中flag含义解释工具
    单端测序(Single- ead)和双端测序(Pai ed-end和Mate-pai )的关系
    区别samtools faid产生的.fai文件功能和bwa index 产生的四个文件的功能
    Perl新接触的小命令
    Perl调用外部命令(其他脚本、系统命令)的方法和区别
    Linux——命令
    学习《Python金融实战》中文版PDF+英文版PDF+源代码
    学习《深度学习与计算机视觉算法原理框架应用》《大数据架构详解从数据获取到深度学习》PDF代码
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7375814.html
Copyright © 2011-2022 走看看