HDU-3237 (like pressure DP)

Topic: the Click
meaning of the questions: Given the height of n books, if there is a continuous range is shown as the same height as a confusing degree (accounting for only a also a degree of disorder), tell you to put it back after k could get the book, ask The minimum degree of confusion.

Described in the book entitled height of 25-32, and n plus the size k, the state may be compressed DP,
again each book observed, when the i-th book we can choose to take or not take, 25 -32 compressed state, since each of the i take the book needs to be considered when a state before a book, and then set a height of the last book, dp [i] [j] [s] [h]: indicates the front i I took the book (0-1 << 8) of least confusion of the last book height h j out of the remaining books present state s.
Analyze the state transition equation:
when i first book I take the time to choose:
I choose not to take the i-th book, but need to determine the height of the last book before it are the same.

Sophisticated sophisticated sophisticated shaped pressure DP, all books state represented by a variable, then the variable with enum s are compared with each other, or can be exclusive to take the book, each represents change it away 0-1 variation confusion here cut the book, need only count the number of the exclusive oR result of 1, i.e., the degree of disorder.
Austrian title card memory, noting that only two of the state transition with each other, the array can be changed to scroll.

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
int n,k;
int a[110];
int dp[3][110][(1<<8)][10];// 前i个  拿走了j个 剩余的书本的状态s 最后一位的高度temp 的mess MIN
int main()
{
    int yy=1;
    while(~scanf("%d %d",&n,&k)&&(n||k))
    {
        memset(dp,inf,sizeof(dp));
        int i,j,s;
        int hh=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]-=25;
            hh|=(1<<a[i]);
        }
        dp[1][1][0][8]=0;//没有任何书放在桌子上
        dp[1][0][0|(1<<a[1])][a[1]]=1;
        for(i=2;i<=n;i++)
        {
            memset(dp[(i)%2],inf,sizeof(dp[(i)%2]));
            for(j=0;j<=k&&j<i;j++)
            {
                for(s=0;s<(1<<8);s++)
                {
                    for(int temp=0;temp<=8;temp++)
                    {
                        if(dp[(i+1)%2][j][s][temp]==inf)//不存在这种状态
                        continue;
                        if(j<k)//取走当前得这本书
                        {
                            dp[i%2][j+1][s][temp]=min(dp[i%2][j+1][s][temp],dp[(i+1)%2][j][s][temp]);
                        }
                        //不拿
                        if(a[i]==temp)
                        {
                            dp[i%2][j][s][temp]=min(dp[i%2][j][s][temp],dp[(i-1)%2][j][s][temp]);
                        }
                        else
                        dp[i%2][j][s|(1<<a[i])][a[i]]=min(dp[i%2][j][s|(1<<a[i])][a[i]],dp[(i-1)%2][j][s][temp]+1);
                    }
                }
            }
        }
        int ans=n;
        for(j=0;j<=k;j++)
        {
            for(s=0;s<(1<<8);s++)
            {
                for(int temp=0;temp<8;temp++)
                {
                    int cnt=0;
                    int tmp=hh^s;
                    while(tmp)
                    {
                        if(tmp&1)
                            cnt++;
                        tmp>>=1;
                    }
                    ans=min(ans,dp[n%2][j][s][temp]+cnt);
                }
            }
        }
        printf("Case %d: %d\n\n",yy++,ans);
    }
    return 0;
}

Published 72 original articles · won praise 19 · views 7496

Guess you like

Origin blog.csdn.net/weixin_43958964/article/details/104957433