POJ 2836 Rectangular Covering(状压dp)

来源:互联网 发布:华彩人生软件 编辑:程序博客网 时间:2024/05/18 00:32

这题看了题解..
明确dp的两个性质.
从某个最优状态,才能确保推到的状态一定是最优的.

状压dp这种多重循环嵌套的正确性在于,0~(1<< n)的遍历顺序,确保了所有被更新到的状态,都不可能在转移到他的状态之前被遍历到.

首先明确两个题目信息
1.假如一个矩形要覆盖两个平行点,那么至少要长度or宽度为1,不是一条线.
2.所有矩形重复计算.
想要知道怎么样来转移,想着一个点一个点来推,dp真是学不会啊T^T.
首先,总共有C(n,2)种矩形可能出现,我们就遍历0~1<< n,(都懂吧?)
再遍历每一个矩形,来求转移后的状态.

/*  xzppp  */#include <iostream>#include <vector>#include <cstdio>#include <string.h>#include <algorithm>#include <queue>#include <map>#include <string>#include <cmath>#include <bitset>#include <iomanip>using namespace std;#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define MP make_pair#define PB push_backtypedef long long  LL;typedef unsigned long long ULL;typedef pair<int,int > pii;typedef pair<double,double > pdd;typedef pair<double,int > pdi;const int MAXN = 1000+3;const int MAXM = 20;const int MAXV = 2*1e3+17;const int BIT = 15+3;const int INF = 0x3fffffff;const LL INFF = 0x3fffffffffffffff;const int MOD = 1e9+7;int dp[1<<BIT];struct rec{    int cov,s;}all[MAXN];int main(){    #ifndef ONLINE_JUDGE     FFF    #endif    int n;    while(cin>>n&&n)    {        vector<pii > ps;        for (int i = 0; i < n; ++i)        {            int x,y;            cin>>x>>y;            ps.PB(MP(x,y));        }        int x = -1;        for (int i = 0; i < n; ++i)        {            for (int j = i+1; j < n; ++j)            {                int lx = ps[i].first,ly = ps[i].second;                 int rx = ps[j].first,ry = ps[j].second;                 if(lx>rx) swap(lx,rx);                if(ly>ry) swap(ly,ry);                              x++;                all[x].s = all[x].cov = 0;                all[x].s = max(1,abs(rx-lx))*max(1,abs(ry-ly));                // if(lx==rx)                //  all[x].s = 1LL*(ry-ly);                // else if(ly==ry)                //  all[x].s = 1LL*(rx-ry);                // else all[x].s = 1LL*(ry-ly)*(rx-lx);                for (int k = 0; k < n; ++k)                {                    int nx = ps[k].first,ny = ps[k].second;                         if(nx>=lx&&nx<=rx&&ny>=ly&&ny<=ry)                        all[x].cov |= 1<<k;                }            }        }        for(int i = 0;i< 1<<BIT ;++i)            dp[i] = INF;        dp[0] = 0;        for (int s = 0; s < 1<<n; ++s)        {            for (int i = 0; i <= x; ++i)            {                int next = s|all[i].cov;                dp[next] = min(dp[next],dp[s]+all[i].s);            }        }        cout<<dp[(1<<n)-1]<<endl;    }    return 0;}
原创粉丝点击