zoj 2860 - Breaking Strings 区间dp优化(四边形不等式)

L - Breaking Strings

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

Description

A certain string-processing language allows the programmer to break a string into two pieces. Since this involves copying the old string, it costs n units of time to break a string of n characters into two pieces. Suppose a programmer wants to break a string into many pieces. The order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20 character string after characters 3, 8, and 10 (numbering the characters in ascending order from the left-hand end, starting from 1). If the breaks are made in left-to-right order, then the first break cost 20 units of time, the second break costs 17 units of time, and the third breaks costs 12 units of time, a total of 49 units of time (see the sample below). If the breaks are made in right-to-left order, then the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, a total of 38 units of time.

The cost of making the breaks in left-to-right order:

thisisastringofchars     (original)
thi sisastringofchars    (cost:20 units)
thi sisas tringofchars   (cost:17 units)
thi sisas tr ingofchars  (cost:12 units)
                         Total: 49 units.

 

The cost of making the breaks in right-to-left order:

thisisastringofchars     (original)
thisisastr ingofchars    (cost:20 units)
thisisas tr ingofchars   (cost:10 units)
thi sisas tr ingofchars  (cost: 8 units)
                         Total: 38 units.

 

Input:

There are several test cases! In each test case, the first line contains 2 integers N (2<=N<=10000000) and M (1<=M<=1000, M<N). N is the original length of the string, and M is the number of the breaks. The following lines contain M integers Mi (1<=Mi<N) in ascending order that represent the breaking positions from the string's left-hand end.

Output:

For each test case, output in one line the least cost to make all the breakings.

Sample Input:

20 3
3 8 10

Sample Output:

37

题意:在一个长度为N的字符串上切M次,第i次切在第ai个字符后面,需要花费被切字符串长度,问怎么切花费最少。

 思路:最后一定是变成M+1个字符串,反过来看就是将m+1个字符串合并,和区间dp的经典问题石子合并一模一样。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define ArrInit(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define ArrInit2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f

typedef long long LL;
typedef long long LD;
using namespace std;
const int maxn=1000+11;
int N,M,K;
LL a[maxn];
LL dp[maxn][maxn];
int p[maxn][maxn];
int f[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
/*
8e7t8fsgf
AC
*/
/*
bool OK(int x,int y)
{
    if(x>=1&&x<=N&&y>=1&&y<=M)return true;
    return false;
}*/

int main()
{
    while(~scanf("%d%d",&M,&N))
    {
        memset(dp,0,sizeof(dp));
        a[0]=0;
        fora2(i,1,N){int x;scanf("%d",&x);a[i]=x;p[i][i]=i;}
        N++;a[N]=M;p[N][N]=N;
        fora2(i,2,N)
        {
            fora2(j,1,N)
            {
                int en=j+i-1;
                if(en>N)break;
                LL sum=INF;
                fora2(k,p[j][en-1],p[j+1][en])
                {
                    
                    LL tem=dp[j][k]+dp[k+1][en]+a[en]-a[j-1];
                    
                    if(sum>tem)
                    {
                        sum=tem;
                        p[j][en]=k;
                    }
                }
                dp[j][en]=sum;
                
            }
        }
        printf("%lld\n",dp[1][N]);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值