ACM暑期集训14

今天刷了两道区间dp的进阶题,再次写下题解。

1. HDOJ 2513 Cake slicing

Cake slicing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 283    Accepted Submission(s): 139

 

Problem Description

A rectangular cake with a grid of m*n unit squares on its top needs to be slicedinto pieces. Several cherries are scattered on the top of the cake with at mostone cherry on a unit square. The slicing should follow the rules below:
1.  each piece is rectangular or square;
2.  each cutting edge is straight and along a grid line;
3.  each piece has only one cherry on it;
4.  each cut must split the cake you currently cut two separate parts

For example, assume that the cake has a grid of 3*4 unit squares on its top,and there are three cherries on the top, as shown in the figure below.

 

One allowable slicing is as follows.

 

For this way of slicing , the total length of the cutting edges is 2+4=6.
Another way of slicing is

 


In this case, the total length of the cutting edges is 3+2=5.

Give the shape of the cake and the scatter of the cherries , you are supposedto find
out the least total length of the cutting edges.

 

 

Input

The input file contains multiple test cases. For each test case:
The first line contains three integers , n, m and k (1≤n, m≤20), where n*m isthe size of the unit square with a cherry on it . The two integers showrespectively the row number and the column number of the unit square in thegrid .
All integers in each line should be separated by blanks.

 

 

Output

Output an integer indicating the least total length of the cutting edges.

 

 

Sample Input

3 4 3

1 2

2 3

3 2

 

 

Sample Output

Case 1: 5

 

分析:

用dp[i][j][k][l]表示以(i,j)为左上角,(k,l)为右下角的矩形切成每份一个樱桃的最小切割长度。然后就利用区间DP的性质,枚举切割点,从小区间转移到大区间。由于这道题不同区间樱桃个数不同,故用递归的写法会方便些。

 

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;

int dp[25][25][25][25];
bool vis[25][25];

int fun(int a,int b,int c,int d)
{
    if(dp[a][b][c][d]!=-1)
        return dp[a][b][c][d];
    int cnt=0,mi;
    for(int i=a;i<=c;i++)
        for(int j=b;j<=d;j++)
        if(vis[i][j]) cnt++;
    if(cnt<=1)
        return dp[a][b][c][d]=0;
    for(int i=a;i<c;i++)
    {
        mi=min(mi,fun(a,b,i,d)+fun(i+1,b,c,d)+d-b+1);
    }
    for(int i=b;i<d;i++) 
    {
        mi=min(mi,fun(a,b,c,i)+fun(a,i+1,c,d)+(c-a+1));
    }
    return dp[a][b][c][d]=mi;

}
int main()
{
    int m,n,k,t=0;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        t++;
        memset(vis,0,sizeof(vis));
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=k;i++)
          {
              int a,b;
              scanf("%d%d",&a,&b);
              vis[a][b]=1;
          }
        int ans=fun(1,1,n,m);
        printf("Case %d: %d\n",t,ans);
    }
    return 0;
}

 

String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 4691    Accepted Submission(s): 2208

 

Problem Description

There are two strings A and B with equal length. Both strings are made up of lowercase letters. Now you have a powerful string painter. With the help of thepainter, you can change a segment of characters of a string to any othercharacter you want. That is, after using the painter, the segment is made up ofonly one kind of character. Now your task is to change A to B using stringpainter. What’s the minimum number of operations?

 

Input

Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.

 

Output

A single line contains one integer representing the answer.

 

Sample Input

zzzzzfzzzzz

abcdefedcba

abababababab

cdcdcdcdcdcd

 

Sample Output

6

7

分析:

先假设A和B所有的对应位置都不相等,然后令dp[l][r]表示l~r这个区间最小操作的次数

然后对一个区间[l ,r] 我们可以考虑,首先将dp[l][r]=dp[l+1][r]+1;表示需要在上一个区间的基础上,额外操作一次。然后枚举一个k

从l+1到r,如果b[k]==b[l] 那么 l这个位置 就可以在刷k的时候 刷上,而且不影响之前的区间。

既 dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r])

然后,我们令ans[i]表示A的第1到第i 刷成和B相同的 需要的最小的操作次数

若a[i]==b[i] 则 ans[i]=ans[i-1] (因为不需要刷)

else 枚举一个j 表示这个ans[i]从哪个答案转移过来

ans[i]=min(ans[i],ans[j]+dp[j+1][i]);

 

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;

char a[110],b[110];
int dp[110][110];
int ans[110];

int main()
{
    while(~scanf("%s",a+1))
	{
		int n=strlen(a+1);
		scanf("%s",b+1);
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
        {
            dp[i][j]=j-i+1;
        }
        for(int j=1;j<=n;j++)
        for(int i=j;i>=1;i--)
        {
            dp[i][j]=dp[i+1][j]+1;
            for(int k=i+1;k<=j;k++)
            {
                if(b[i]==b[k])
                {
                    dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            ans[i]=dp[1][i];
            if(a[i]==b[i])
            {
                ans[i]=min(ans[i],ans[i-1]);
            }
            else
            for(int j=1;j<i;j++)
            {
                ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
            }
        }
        printf("%d\n",ans[n]);

	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值