zoukankan      html  css  js  c++  java
  • 状态压缩---区间dp第一题

    标签: ACM


    题目

    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 A before 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题,看得我头有点晕
    题意为给你几个需要穿的衣服编号你需要依次穿上,可以套着穿,但是拖了不能再穿同一件(要增加次数),求最少的衣服数量
    从最后一天往第一天dp,可以选择直接套上则dp[i][j]=dp[i+1][j]+1,如果中间有相同一套的衣服,可以选择把衣服脱了,则dp[i][j]=dp[i+1][k]+dp[k+1][j],再对这两个值取最小值即为dp[i][j]最小值

    AC代码

    #include <iostream>
    #include <string.h>
    using namespace std;
    int dp[105][105];
    int dress[105];
    int main()
    {
        int t,n,i,j,k,x;
        while(cin>>t)
        for(i=1;i<=t;i++)
        {
            memset(dp,0,sizeof(dp));
            cin>>n;
            for(j=0;j<n;j++)
                cin>>dress[j];
            for(j=0;j<n;j++)
                dp[j][j]=1;
            for(j=n-1;j>=0;j--)
                for(k=j+1;k<n;k++)
            {
                dp[j][k]=dp[j+1][k]+1;
                for(x=j+1;x<=k;x++)
                    if(dress[x]==dress[j])
                    dp[j][k]=min(dp[j][k],dp[j+1][x]+dp[x+1][k]);
            }
            cout<<"Case "<<i<<": "<<dp[0][n-1]<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    海量数据中,寻找最小的k个数。
    快速排序
    反转一个单链表,分别以迭代和递归的形式来实现
    N个大小不等的自然数排序,时间复杂度为O(n),空间复杂度为O(1)
    堆排序
    两个已经排好序的链表合并为一个有序链表
    字符串过滤空格、回车、tab
    求一个浮点数的连续子序列最大乘积 (2013 小米校园招聘笔试题)
    单向循环链表队列,从头开始报数,当报到m或者m的倍数的元素出列
    给一个数组,元素都是整数(有正数也有负数),寻找连续的元素相加之和为最大的序列。
  • 原文地址:https://www.cnblogs.com/allen-lzl/p/7932233.html
Copyright © 2011-2022 走看看