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
*/


你可能感兴趣的:(hdu 5115 区间dp)