帮助BSNY(状态压缩+4维dp+滚动数组)

懵逼题,一度推出六维的DP,最后看了题解。。

恍然大悟。。。(需要运用好题目的限制(a[i]>=25 且a[i]<=32))并将相同的a[i]进行压缩,压缩成一个值

因为拿出一本书只有两种可能,(1)放到最前面,(2)放到与它相同编号的书的旁边,那么我们可以就此加上限制,就可以推出状态转移方程式了(PS:每次拿书必须是一团一团地取出来,否则并不改变原状态)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

ll n,k,a[10005],h[100005],x,ans,flag[100005],sum[10005],f[2][105][10][1<<8],tot,all,p[10005];

int main(){
    scanf("%lld%lld",&n,&k);
    for (int i=1; i<=n; i++) scanf("%lld",a+i),a[i]-=24;
    for (int i=1; i<=n; i++)
    {
        if (a[i]!=a[i-1]) h[++tot]=a[i];
        sum[tot]++;
    }
    for (int i=tot; i; i--){
        if (flag[h[i]]) p[i]=1;
        flag[h[i]]=1;
    }
    memset(f[0],127,sizeof(f[0]));
    f[0][0][0][0]=0;
    all=(1<<8)-1;
    for (int i=1; i<=tot; i++)
    {
        x=x^1;
        memset(f[x],127,sizeof(f[x]));
        for (int j=0; j<=k; j++)
            for (int w=0; w<=8; w++)
                for (int m=0; m<=all; m++)
                {
                    if (f[x^1][j][w][m]>n) continue;
                    f[x][j][h[i]][m|(1<<(h[i]-1))]=min(f[x][j][h[i]/*当前的最后一位是h[i]*/][m|(1<<(h[i]-1))],f[x^1][j][w][m]+(h[i]!=w));//当前的状态等于之前的状态+(判断此时扫描的位是否等于原先的最后一位)
                    if (j+sum[i]>k) continue;
                    f[x][j+sum[i]][w][m|(1<<(h[i]/*将所有的相同值的合并后得到的*/-1)/*h[i]-1表示h[i]是否被取过了,与mor一下表示当前的状态*/)]=min(f[x][j+sum[i]][w][m|(1<<(h[i]-1))],f[x^1][j][w][m]+!(m&(1<<(h[i]-1))));
                    if (p[i]) f[x][j+sum[i]/*改变为j+sum[i]次之后的状态*/][w][m]=min(f[x][j+sum[i]/*改变为j+sum[i]次之后的状态*/][w][m],f[x^1][j][w][m]);
                }
    }
    //每次x进行^操作是为了创造出滚动数组,即本次的状态只跟上一次的状态有关
    ans=124278904761894269;
    for (int i=0; i<=k; i++)
        for (int w=0; w<=8; w++)
            for (int m=0; m<=all; m++)
            ans=min(ans,f[x][i][w][m]);
    printf("%lld\n",ans);
    return 0;
}
时间: 2024-10-13 13:22:24

帮助BSNY(状态压缩+4维dp+滚动数组)的相关文章

hdu4336之状态压缩慨率DP

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2141    Accepted Submission(s): 1008 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

(hiho1048)POJ2411Mondriaan&#39;s Dream(DP+状态压缩 or 轮廓DP)

问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangle

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

HDU - 1024 Max Sum Plus Plus(dp+滚动数组优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. 题解:dp[i][j]从前j个数字中选择i段,然后根据第j个数字是否独立成一段,可以写出 状态转移方程:dp[i][j]=max(dp[i][j-1]+num[j],max(dp[i-1][k])+num[j]) 这里的max(dp[i-

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the