区间dp

来源:互联网 发布:持有期收益率知乎 编辑:程序博客网 时间:2024/06/06 02:24

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;    }}
0 0
原创粉丝点击