[UVA12235] Help Bubu thinking title + state definition + Dp

Online JudgeUVA12235

The Label : thinking problems, state definitions, like pressure Dp

Questions surface:

Title Description

There is a shelf, put the top n book, from left to right of i height of the book to H [i] . Disorder is defined as the number of consecutive shelves contour segments.

For example: {30,30,31,31,32} confusion of 3; {30,32,32,31} confusion of 3; 31,32,31,32,31 {5} degree of chaos .

Now you can extract from up to k book back on the shelf and then they are free anywhere, seeking a final minimum degree of confusion.

To 100% of the data, N the range [1,500], K range [1,100], the height of the scope of the [25, 32];

Entry

The first line of two integers N, K;

The second line, N elements, from left to right represents the height of each book.

Export

Relocate the minimum degree of disorder output K of the book.

"Print a blank line after the output of each case."Note that each set of output data to two wrap! !

Sample

Input

5 2
25 25 32 32 25
5 1
25 26 25 26 25
0 0

Output

Case 1: 2
Case 2: 3

answer

It found that the height of the book only \ ([25,32] \) of these 8, it seems like the pressure the way. Renumbered height \ (0 \) ~ \ (7 \) conveniently expressed later, and is referred to \ (8 \) different colors.

The definition of state \ (dp [i] [J] [the STA] [CO] \) , before considering only \ (i \) book, which has been \ (j \) This book took up (note that this is picked up , in fact, is to adopt a perspective of God, first of all out and then put it back to a time a), \ (the STA \) represents the color of the book which has not been picked up (expressed in binary state), \ (CO \) represents the last did not take up the color of the book, Dp value represents at this time (and before i) the rest of the book to the confusion of those not moving.

However, you will find this a direct array will be given MLE, how to optimize it,As long as you pull down the solution to a problem that directly lookTransfer process, you will find, for the first dimension is a time to spend one state, so you can scroll through it the first dimension. Le this space is no problem.

First to say something about how the next realization:

Initial State: for any \ (h∈ [h [1] , h [n]] \) have \ (dp [i] [i -1] [1 << h] [h] \) represents, in addition to this current book, the foregoing \ (i-1 \) state have to take up the book;

Final state: Statistics \ (dp [n] [j ] [sta] [co] + val \) a minimum value, wherein the \ (Val \) = \ (All \) \ (Xor \) \ (STA \) in number 1, in fact, the total number of those kinds of color take up the book.

And it can transfer into the side performed while reading. For the current height \ (H \) (-24 first converted to a digital 0 to 7), we enumerate \ (I, STA, CO.'S \) , see note below:

inline void Do(int &x,int y){if(x==-1||x>y)x=y;}
//枚举第o本书
int g=0,all=0;
Rep(o,1,n){
        //读入高度,预处理下
        int h;scanf("%d",&h);h-=25;
        //滚动~
        g^=1;memset(dp[g],-1,sizeof(dp[g]));
        //上面所说的初态初始化。注意要判:o-1<=m,因为第二维我们只开到m的上限100,不特判可能会数组越界
        if(o-1<=m)dp[g][o-1][1<<h][h]=1; 
    
        //i:已拿个数 s:未选颜色状态 c:最近一次没动的书的颜色 
        Rep(i,0,min(o,m))Rep(s,1,all)Rep(c,0,7){//枚举上一层状态
            if(dp[g^1][i][s][c]==-1)continue;
             //最近的位置上有一个跟h颜色相同的书没拿走,很明显把当前这个留下来较优
            if(h==c)Do(dp[g][i][s][c],dp[g^1][i][s][c]);
            else{
                 //把当前这个留在原位,由于前面一个跟自己颜色不同,所以混乱值+1
                Do(dp[g][i][s|(1<<h)][h],dp[g^1][i][s][c]+1);
                //把当前这个拿起来
                Do(dp[g][i+1][s][c],dp[g^1][i][s][c]); 
            }
        }
        all|=1<<h;//all表示当前已经含有的颜色
    }

Final statistics based on the above 终态to just fine.

The complete code:

#include<bits/stdc++.h>
#define Rep(a,b,c) for(int a=b;a<=c;++a)
using namespace std;
int n,m,dp[2][115][260][10];
inline void Do(int &x,int y){
    if(x==-1||x>y)x=y;
}
int main(){
    int cas=0;
    while(~scanf("%d%d",&n,&m)){
    if(n==0||m==0)return 0;memset(dp,-1,sizeof(dp));
    int g=0,all=0;
    Rep(o,1,n){
        int h;scanf("%d",&h);h-=25;
        g^=1;memset(dp[g],-1,sizeof(dp[g]));
        if(o-1<=m)dp[g][o-1][1<<h][h]=1;
        Rep(i,0,min(o,m))Rep(s,1,all)Rep(c,0,7){
            if(dp[g^1][i][s][c]==-1)continue;
            if(h==c)Do(dp[g][i][s][c],dp[g^1][i][s][c]);
            else{
                Do(dp[g][i][s|(1<<h)][h],dp[g^1][i][s][c]+1);
                Do(dp[g][i+1][s][c],dp[g^1][i][s][c]);
            }
        }
        all|=1<<h;
    }
    int ans=n;
    Rep(i,0,m)Rep(s,1,all)Rep(c,0,7)if(~dp[g][i][s][c]){
        int cnt=0,choose=all^s;
        Rep(o,0,7)if((1<<o)&choose)cnt++;
        ans=min(ans,dp[g][i][s][c]+cnt);
    }
    printf("Case %d: %d\n\n",++cas,ans);
    }
}

End

Re-read through this question, the main difficulty lies in properly defined state . Like pressure, scroll both optimized for the range of data and transfer process is not difficult to think. Why can traverse past time transferred it to the rear in this order, the examination also have thought about such an order, but immediately able to find counter-examples - may book can also be placed in front of the block after him in. In fact, still not figured that God point of view, coming out of these books to the store, to a greedy when a plug ♂ back, in no hurry to determine which block in the end should be placed inside.

Guess you like

Origin www.cnblogs.com/Tieechal/p/11402111.html