区间dp

http://acm.hust.edu.cn/vjudge/contest/121485#problem/A

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.
这里写图片描述

#include <cstdio>
#include <iostream>
#include <cstring>
int a,b[210],dp[210][210];
using namespace std;

int sov(int l,int r)
{
    if(dp[l][r] != INT_MAX)
        return dp[l][r];
    if(l == r-1)
        return 0;
        int minn = INT_MAX;
    for (int k = l+1;k< r ;k++)
        minn = min(minn,sov(l,k)+sov(k,r));
    dp[l][r] = minn+b[l]+b[r];
    return dp[l][r];
}

int main()
{
    int T;
    cin >> T;
    for (int ca = 1; ca <= T ; ca++)
    {
        printf("Case #%d: ",ca);
        int n,ans = 0;
        cin >> n;
        for(int i = 0 ; i <= n+1 ;i++)
            for (int j = 0 ; j <= n+1 ;j++)
                dp[i][j] = INT_MAX;
        for (int i = 1; i <= n ; i++)
           {
                cin >> a;
                ans += a;
           }
        for (int i = 1; i <= n ;i++)
            cin >> b[i];
        b[0] = 0;
        b[n+1] = 0;
        sov(0,n+1);
        cout <<ans+dp[0][n+1] << endl;
    }
}

http://acm.hust.edu.cn/vjudge/contest/121485#problem/B
这里写图片描述
Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor.
Dire wolves look like normal wolves, but these creatures are of nearly twice the size. These powerful beasts, 8 - 9 feet long and weighing 600 - 800 pounds, are the most well-known orc mounts. As tall as a man, these great wolves have long tusked jaws that look like they could snap an iron bar. They have burning red eyes. Dire wolves are mottled gray or black in color. Dire wolves thrive in the northern regions of Kalimdor and in Mulgore.

#include <cstdio>
#include <iostream>
#include <cstring>
int a,b[210],dp[210][210];
using namespace std;

int sov(int l,int r)
{
    if(dp[l][r] != INT_MAX)
        return dp[l][r];
    if(l == r-1)
        return 0;
        int minn = INT_MAX;
    for (int k = l+1;k< r ;k++)
        minn = min(minn,sov(l,k)+sov(k,r));
    dp[l][r] = minn+b[l]+b[r];
    return dp[l][r];
}

int main()
{
    int T;
    cin >> T;
    for (int ca = 1; ca <= T ; ca++)
    {
        printf("Case #%d: ",ca);
        int n,ans = 0;
        cin >> n;
        for(int i = 0 ; i <= n+1 ;i++)
            for (int j = 0 ; j <= n+1 ;j++)
                dp[i][j] = INT_MAX;
        for (int i = 1; i <= n ; i++)
           {
                cin >> a;
                ans += a;
           }
        for (int i = 1; i <= n ;i++)
            cin >> b[i];
        b[0] = 0;
        b[n+1] = 0;
        sov(0,n+1);
        cout <<ans+dp[0][n+1] << endl;
    }
}

http://acm.hust.edu.cn/vjudge/contest/121485#problem/C
Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag’s contents are currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).

Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is “abcba” would read the same no matter which direction the she walks, a cow with the ID “abcb” can potentially register as two different IDs (“abcb” and “bcba”).
这里写图片描述

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int dp[2010][2010];
int cost[30];

int main()
{
    int n,m;

    while(cin >> n>>m)
    {
        char s[2010];
        scanf("%s",s);
        char a;
        int x,y;
        for (int i = 0 ; i < n ;i++)
        {
            cin >> a>>x >>y;
            cost[a-'a'] = min(x,y);
        }
        memset(dp,0,sizeof(dp));
        for (int k = 1 ; k <m ; k++)
            for (int l = 0,r = k; r < m; r++,l++)
            {
                    if(s[l] == s[r])
                        dp[l][r] = dp[l+1][r-1];
                   else
                        dp[l][r] = min(dp[l+1][r]+cost[s[l]-'a'],dp[l][r-1]+cost[s[r]-'a']);
            }
        cout << dp[0][m-1]<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值