您的位置:首页 > 其它

LightOj 1422

2015-11-09 19:55 330 查看
1422 - Halloween Costumes



PDF (English)StatisticsForum
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 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

Output for Sample Input

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Case 1: 3

Case 2: 4

 

这道题苦思冥想,终于似乎知道怎么解了,结果wr了,不断修改,直到感觉真的没错了,还是wr,最后才发现是题中描述有错误,ci的有等于0的。

You are a liar!!!!

其实就是先合并,把相同的且相邻的合并成一个,然后根据动态转移方程走就可以了。(正如第一个链接中的动态转移方程所示。)

其实区间dp什么的,说白了,还是dp,找动态转移方程,没什么特殊的。

ac的代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 110

int num
;
int place

;
int dp

;

int main(){
int t;
int n;
int m;
int tempnum;

scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
scanf("%d",&n);

num[0]=-1;
m=1;
memset(place,0,sizeof(place));
for(int i=0;i<n;i++){
scanf("%d",&tempnum);

/*if(tempnum==0){加上这句时,re了。
printf("%d\n",1/0);
}*/

if(tempnum!=num[m-1]){
num[m]=tempnum;
place[tempnum][++place[tempnum][0]]=m;
m++;
}

/*scanf("%d",&num[i+1]);
place[num[i+1]][++place[num[i+1]][0]]=i+1;*/
}
/*m=n+1;*/

/*for(int i=0;i<m;i++){
printf("%d ",num[i]);
}
printf("\n");*/

memset(dp,0,sizeof(dp));
for(int i=1;i<m;i++){
dp[i][i]=1;
}

/*for(int i=1;i<m;i++){
for(int j=1;j<m;j++){
printf("%d ",dp[i][j]);
}
printf("\n");
}*/

for(int i=2;i<=m-1;i++){
for(int j=1;j<=m-i;j++){
/*if(i+j-1==j){
printf("wo shi da hao ren\n");
}*/

/*for(int i=1;i<m;i++){
for(int j=1;j<m;j++){
printf("%d ",dp[i][j]);
}
printf("\n");
}
printf("hahaha\n");*/

dp[j][i+j-1]=dp[j+1][i+j-1]+1;

//printf("%d %d %d ",dp[j+1][i+j-1],j,i+j-1);
for(int k=1;k<=place[num[j]][0];k++){
int temp=place[num[j]][k];

//printf("%d %d %dha ",place[num[j]][0],k,temp);
if(temp<=j||temp>i+j-1){
//printf("hahaha\n");
continue;
}
else{
/*if(j+1==temp){
printf("%d\n",1/0);
}
if(j+1==temp){
dp[j][i+j-1]=min(dp[j][i+j-1],dp[temp][i+j-1]);
}
else{*/
dp[j][i+j-1]=min(dp[j][i+j-1],dp[j+1][temp-1]+dp[temp][i+j-1]);//dp[j+1][temp-1]+dp[temp+1][i+j-1]-1变成了dp[j+1][temp-1]+dp[temp][i+j-1]
/*}*/
}
}
}
}

printf("Case %d: %d\n",cas,dp[1][m-1]);
}

return 0;
}


参考链接:
http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/archive/2013/08/04/3236035.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: