POJ 2836 Rectangular Covering(状压dp)

这题看了题解..
明确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_back
typedef 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值