Dynamically plan the appearance level (mg) Questions and solutions, and mg questions
A company carries a batch of boxes in N (1 ≤ N ≤ 1000) in sequence by "conveyor belt", and then arranges at most P (1 ≤ P ≤ 4) columns in the warehouse, (8 ). Currently, we know that a maximum of M (1 ≤ M ≤ 20) boxes can be shipped. We want to sort the boxes of the same type together as much as possible to make them beautiful. "Beauty degree" T is defined as: T = Σ (the number of different types seen in each column in sequence); the so-called "Number of different types seen in sequence" is: if the K box in a column is different from the K-1 box type, the value of "Beauty level" is increased by 1. Find a transfer arrangement to minimize the "Beauty level" value of each column .! Http://img.blog.csdn.net/20150828211629851)
The difficulty of this question is how to compress the status. We can use m + 1 hexadecimal numbers for State compression. Then, a dfs function is used to search for the status for status transfer. F [I] [j] Indicates the minimum aesthetic level when the status of each column of the First I number is compressed to j.
Additional code (original code by TA god. Reprinted with the source ):
#include<cstdio>#include<cstring>#include<iostream>using namespace std;#include<algorithm>#define S 21int f[2][194485],p,now,pre=1,mi[5],m,a;void out(int state){ for(int i=0;i<4;++i)cout<<state%mi[i+1]/mi[i]<<" "; cout<<endl;}void dfs(int x,int state){ if(x<0){ if(f[pre][state]<1000){ int i; for(i=p;i--;){ x=state%mi[i+1]/mi[i]; f[now][state+(a-x)*mi[i]]=min(f[now][state+(a-x)*mi[i]],f[pre][state]+(x!=a)); } } return; } for(int i=m;~i;--i)dfs(x-1,state*(m+1)+i);}int main(){ freopen("mg.in","r",stdin); freopen("mg.out","w",stdout); int n,i; scanf("%d%d%d",&n,&m,&p); mi[0]=1; for(i=1;i<5;++i)mi[i]=mi[i-1]*(m+1); memset(f,127,sizeof(f)); f[0][0]=0; while(n--){ scanf("%d",&a); swap(now,pre); memset(f[now],127,sizeof(f[now])); dfs(p,0); } printf("%d\n",*min_element(f[now],f[now+1]));}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.