[动态规划] bzoj1806: [Ioi2007]Miners 矿工配餐

20 篇文章 0 订阅

bzoj 1806: [Ioi2007]Miners 矿工配餐 http://www.lydsy.com/JudgeOnline/problem.php?id=1806

f[2][4][4][4][4]:f[这是一个滚动数组][a][b][c][d] a,b:第一个坑最近两辆车 c,d:第二个坑最近两辆车
如果开到100000的话 64M的内存爆的稳稳的 要开滚动数组
搞memset搞了很久

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[2][4][4][4][4];//f[这是一个滚动数组][a][b][c][d] a,b:第一个坑最近两辆车 c,d:第二个坑最近两辆车
char ch[110000];
int main()
{
    int n,now=1,last=0,ans=0;
    scanf("%d",&n);
    scanf("%s",ch+1);
    memset(f[now],0xef,sizeof(f[now]));
    f[now][0][0][0][0]=0;
    for (int i=1;i<=n;i++)
    {
        int x;
        if (ch[i]=='M') x=1; 
        if (ch[i]=='F') x=2;
        if (ch[i]=='B') x=3;
        now=1-now;last=1-last;
        memset(f[now],0xef,sizeof(f[now]));
        /*
        这里要memset成最小值
        至于为什么也试了很久(菜)
        因为max的时候f[a][b][c][d]这个状态有可能是不存在的(因为这个是靠枚举得到的) 
        所以如果初始值为0的话max之后有可能会取到这个根本就不存在的方案导致WA掉
        其实赋给一个小于-3的值也是ok的(s<=3) 
        */ 
        for (int a=0;a<=3;a++)
        {
            for (int b=0;b<=3;b++)
            {
                for (int c=0;c<=3;c++)
                {
                    for (int d=0;d<=3;d++)
                    {
                        int s=1;
                        //分配给第一个坑
                        if (a>0) s++; if (b>0) s++; 
                        if (a==b&&a!=0) s--;
                        if (x==a) s--;
                        if (x==b&&a!=b) s--;
                        f[now][b][x][c][d]=max(f[now][b][x][c][d],f[last][a][b][c][d]+s);
                        //分配给第二个坑  
                        s=1;
                        if (c>0) s++; if (d>0) s++; 
                        if (c==d&&c!=0) s--;
                        if (x==c) s--;
                        if (x==d&&c!=d) s--;
                        f[now][a][b][d][x]=max(f[now][a][b][d][x],f[last][a][b][c][d]+s);
                    }
                }
            }
        }
    }
    for (int a=0;a<=3;a++)
        for (int b=0;b<=3;b++)
            for (int c=0;c<=3;c++)
                for (int d=0;d<=3;d++)
                    ans=max(ans,f[now][a][b][c][d]);
    printf("%d\n",ans);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值