hdu 5115 區間dp

http://acm.hdu.edu.cn/showproblem.php?pid=5115

有一排狼人要全部打死,每個狼人有個攻擊力和光環(增加旁邊狼人的攻擊),打死一頭狼獵人會掉其加了buff以後攻擊力的血量,同時其兩邊的兩個狼自動挨在一起,問最少掉血多少?

區間dp,O(n^3),可以優化一下,但是n只有200..

一開始看成了n有100000..汗.而且時限開那麼打是要鬧哪樣..?

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 205;
map<int,int > hash;
//#define a first
//#define b second
//typedef pair<int,int> p2;

//p2 s[maxn];
int a[maxn],b[maxn],dp[maxn][maxn];
template<class T> T min(T a,T b,T c)
{
    return min(min(a,b),c);
}
int work()
{
    int n;
    RD(n);
    for(int i = 1;i <= n;++i)
        RD(a[i]);
    for(int i = 1;i <= n;++i)
        RD(b[i]);
    b[0] = b[n + 1] = 0;

    for(int i = 1;i <= n;++i)
        dp[i][i] = b[i-1] + b[i+1] + a[i];
    for(int i = 1;i <= n;++i){
        for(int j = 1;j + i <= n;++j){
            dp[j][j+i] = min(dp[j][j+i-1] + a[j+i] + b[j+i+1] + b[j-1], dp[j+1][j+i] + a[j] + b[j-1] + b[j+i+1]);
            for(int k = j + 1;k < j + i;++k)
                dp[j][j+i] = min(dp[j][j+i],dp[j][k-1] + b[j-1] + a[k] + b[j+i+1] + dp[k+1][j+i]);
        }
    }
    return dp[1][n];
}
int main()
{
    int _,cas = 1;
    RD(_);
    while(_--){
        printf("Case #%d: %d\n",cas++,work());
    }
    return 0;
}
/*
2
3
3 5 7
8 2 0
10
1 3 5 7 9 2 4 6 8 10
9 4 1 2 1 2 1 4 5 1
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章