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 }
  • 相关阅读:
    "用脑思考"和"用心感知"
    Oracle 分页查询
    MySQL定时执行存储过程
    java商城小程序收藏
    巴士团小程序
    微信小程序this作用域
    Flink之API的使用(3):Source的使用
    Flink之API的使用(2):Transform算子的使用
    Flink之API的使用(1):Sink的使用
    Flink之基础内容(2):DataStream的创建和使用
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7375814.html
Copyright © 2011-2022 走看看