The 35th ACM/ICPC Asia Regional Hangzhou Site —— Online Contest hdu 3646 Fate Stay Night

原题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3646

前几天同学老是说我的博客里总说什么简单,水题之类的,他们都看不懂,事实上开始的时候我也不懂,但是明白了之后就觉的自己太傻,好吧,今天的这个题确实不简单!算是比较难的,因为我看了好久,并不是因为同学的说!

这个题的特点是状态为二维,但是每一个状态又必须记录一个量!这个是我第一次做!好吧,我见识短!

贴代码!

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

int dp[10010][110];
int rest[10010][110];

int bird[10010];
int life[100010];
int n,m,k,a;
int cal(int att,int cnt,int& res) {
    int flag=-1;
    while( cnt < k ) {
        if( att>res ) {
            att-=res;
            ++cnt;
            res=life[cnt+1];
            flag=1;
        }
        else if( att==res ) {
            ++cnt;
            res=life[cnt+1];
            break;
        }
        else if( att<res ) {
            if( flag==-1 )  res-=att;
            break;
        }
    }
    return cnt;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&k) != EOF && !(!n && !m && !k))
    {
        for(int i = 1;i <= n;i++)
            scanf("%d",&bird[i]);
        for(int i = 1;i <= k;i++)
            scanf("%d",&life[i]);

        memset(rest,0,sizeof(rest));
        memset(dp,0,sizeof(dp));

        rest[0][0] = rest[0][1] = life[1];
        for(int i = 1;i <= n;i++)
        {
            a=rest[i-1][0];
            dp[i][0]=cal(bird[i],dp[i-1][0],a);
            rest[i][0]=a;
            for(int j = 1;j <= m;j++)
            {
                if( j > i ) {
                    dp[i][j]=dp[i][j-1];
                    rest[i][j]=rest[i][j-1];
                    continue;
                }
                //if(j == i) dp[i][j] = dp[i-1][j-1];
                int p = rest[i-1][j];
                int q = rest[i-1][j-1];

                int a = cal(bird[i],dp[i-1][j],p);
                int b = cal(bird[i]*2,dp[i-1][j-1],q);
                if(a > b)
                {
                    dp[i][j] = a;
                    rest[i][j] = p;
                }else if(a < b)
                {
                     dp[i][j] = b;
                     rest[i][j] = q;
                }else
                {
                    dp[i][j] = a;
                    rest[i][j] = min(p,q);
                }
            }
        }
        printf("%d\n",dp[n][m]);
    }
    return 0;
}


 

 

你可能感兴趣的:(The 35th ACM/ICPC Asia Regional Hangzhou Site —— Online Contest hdu 3646 Fate Stay Night)