LightOJ - 1422 (Halloween Costumes)

题目链接:传送门

题目大意:要参加聚会,对应聚会要穿对应衣服,衣服可以套着穿,也可以脱下来,但脱下来之后不能再穿,问参加完所有聚会至少需要几件衣服?

题目思路:区间DP

     一开始自己没有想出来状态转移方程,但是想到了左右区间如果边界相等的时候衣服数目应该-1,可以少穿一件。其实拓展一点就是更新

     方程的一部分了。对于区间 [l,r] ,如果r处的值==l处的值(要求穿的衣服相同),那么区间 [l,r]所穿的最少衣服等于区间 [l,r-1] 所穿最少衣服,

     因为 l 是起点,也就是说最里面穿的衣服是 l 对应的值,从 l 到 r 无论怎么更新,最里面的那件衣服始终不用脱掉,那么到 r 时候,至少有一种

     情况,也就是脱其他所有衣服,只剩 l 时,这时候也是满足条件的。对应的转移的方程 if(a[l]==a[r]) dp[l][r]=dp[l][r-1];

     然后对于其他区间,也就相同于普通区间DP转移。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 100005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL;

int n,m,a[105],dp[105][105];

int main(){
    int i,j,group,Case=0;
    scanf("%d",&group);
    while(group--){
        mst(dp,inf);
        scanf("%d",&n);
        for(i=1;i<=n;++i){
            scanf("%d",&a[i]);
            dp[i][i]=1;
        }
        for(int p=2;p<=n;++p)
        for(int i=1;i<n;++i){
            j=i+p-1;
            if(j>n)break;
            if(a[i]==a[j])dp[i][j]=dp[i][j-1]; ///***
            for(int k=i;k<j;++k){
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
                ///这里如果a[k]==a[k+1]不需要处理
                ///因为在转移之前已经处理过了
            }
        }
        printf("Case %d: %d\n",++Case,dp[1][n]);
    }
    return 0;
}
时间: 2024-08-11 01:27:13

LightOJ - 1422 (Halloween Costumes)的相关文章

【LightOJ 1422】Halloween Costumes(区间DP)

题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第i天之前的衣服最少需要的衣服数量,那就可以由和第j天穿一样的衣服的第k天转移过来,或者再套一件第j天的衣服. 状态转移方程:dp[i][j]=min(dp[i][k]+dp[k+1][j-1],dp[i][j-1]+1)(i≤k<j,a[j]==a[k]) 算的时候i从大到小算,因为算dp[i][j

万圣节问题(Halloween Protection)

万圣节效应指的是结果集中数据移动位置并因此被改变多次.这个效应不同于双读,因为它是有数据修改驱动的,而不是读取查询.要执行一个更行,数据必须先被读取.执行这个要使用两个游标,一个用于读取,另一个用于写入.如果数据在所有的数据读入之前被写入游标更行,那么就有可能出现某行移动位置,并再次被读取,从而再次被更新.理论上,这会永远持续下去.使用索引读取数据,该索引的键值(key)会被查询更新,这是万圣节效应的一个例子.万圣节效应显然非常让人不悦,幸好SQL Server的存储引擎使其免受该效应.要确保可

Finding LCM LightOJ - 1215 (水题)

这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <ve

万圣节的糖果(Halloween Sweets)

今天遇到codewars的一道题,这是链接,讲的是关于万圣节的一个题目,简单点说,就是9个包裹,一个天平,两次称的机会,怎么找出9个包裹中唯一一个较重的包裹. 像我这种年轻时候喜欢研究难题获得存在感的蠢材,觉得很开心,因为这是我为数不多还记得答案的小学题.包裹分成三堆,取两个堆一称,可以得到哪个是比较中的一堆,然后再在这个异常的堆里选择两个称,找到嫌疑犯X. 于是我开始码代码 function pick(bags, scale) { switch(scale.weigh([bags[0],bag

lightoj 1179(线段树)

传送门:Josephus Problem 题意:经典约瑟夫问题,有n个人,每次数到第k个人出列,求剩下的最后一人. 分析:用线段树模拟约瑟夫问题,记录区间的减少情况,然后根据每次数到的人在区间排第几位,线段树log(n)找到并更新,总复杂度为O(nlog(n)). #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define N 100010 #defin

Fantasy of a Summation LightOJ - 1213 (快速幂)

题意: 首先 只看第一层循环的A[0],是不是用了nk-1次  A[1]也是用了nk-1次······ 所以 第一层的sum(A[i]的和) 一共用了nk-1 所以第一层为sum * nk-1 因为又k层循环 所以全部为sum * nk-1 * k 最后不要忘了 % MOD 代码如下: #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #define mem

Lightoj 题目1422 - Halloween Costumes(区间DP)

1422 - Halloween Costumes   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 Ha

LightOJ 1422 Halloween Costumes (区间dp 好题)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, nextweekend is Halloween, and he is planning to attend as many parties as he can.Since it's Hall