传送门

分析

首先我们不难想到1e4^5的暴力枚举,但显然这是不行的,于是我们考虑对于每一张海报肯定有一种最优情况使得它至少有一条边要么靠着板子的边要么靠着之前的某一张海报的边,这样我们便可以将复杂度优化了很多。我们再考虑将每一种情况进行哈希,这样便可以避免了如图一的情况(矩形中的数字是指这个矩形是第几个被添加进来的)

图一

我们看得出来这两种实际是一种情况,所以通过哈希我们可以避免重复搜索。在有了这些之后我们再在程序中加一些其它剪枝,用容斥统计答案就行了,详见代码。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl
#define uli long long
const uli HASH=;
int n,m,k,a[],b[],x[],y[],ans;
set<uli>vis;
uli gethash(int msk){
uli hsh=;
for(int i=;i<k;i++)
if(msk&(<<i)){
hsh=hsh*HASH+(uli)x[i]-(uli)x[];
hsh=hsh*HASH+(uli)y[i]-(uli)y[];
}else {
hsh=hsh*HASH+(uli)n;
hsh=hsh*HASH+(uli)m;
}
return hsh;
}
int getans(int msk){
int i,j,res=;
for(i=msk;i>;i=(i-)&msk){
int x1=-,y1=-,x2=n,y2=m;
for(j=;j<k;j++)
if(i&(<<j)){
x1=max(x1,x[j]),y1=max(y1,y[j]);
x2=min(x2,x[j]+a[j]-);
y2=min(y2,y[j]+b[j]-);
}
if(__builtin_popcount(i)&){
res+=max(x2-x1+,)*max(y2-y1+,);
}else res-=max(x2-x1+,)*max(y2-y1+,);
}
return res;
}
void fx(int);
void work(int msk,int wh,int xx,int yy){
if(yy<||yy+b[wh]>m||xx<||xx+a[wh]>n)return;
x[wh]=xx;y[wh]=yy;
int res=getans(msk|(<<wh));
ans=max(ans,res);
for(int i=;i<k;i++)
if(i!=wh&&!(msk&(<<i)))
res+=a[i]*b[i];
if(res<=ans)return;
uli hsh=gethash(msk|(<<wh));
if(vis.find(hsh)==vis.end()){
vis.insert(hsh);
fx(msk|(<<wh));
}
}
void fy(int msk,int wh,int xx){
int i;
if(xx<||xx+a[wh]>n)return;
work(msk,wh,xx,);
work(msk,wh,xx,m-b[wh]);
for(i=;i<k;i++)
if(msk&(<<i)){
work(msk,wh,xx,y[i]+b[i]);
work(msk,wh,xx,y[i]-b[wh]);
}
}
void fx(int msk){
int i,j;
for(i=;i<k;i++)
if(!(msk&(<<i))){
fy(msk,i,);
fy(msk,i,n-a[i]);
for(j=;j<k;j++)
if(msk&(<<j)){
fy(msk,i,x[j]+a[i]);
fy(msk,i,x[j]-a[i]);
}
}
}
class Posters {
public:
int maxCover(int wt,int ht,vector<int>pw,vector<int>ph){
n=wt,m=ht,k=pw.size();
for(int i=;i<k;i++){
a[i]=min(pw[i],n);
b[i]=min(ph[i],m);
}
fx();
return ans;
}
};

Posters TopCoder - 1684的更多相关文章

  1. POJ2528Mayor's posters[线段树 离散化]

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59683   Accepted: 17296 ...

  2. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  3. POJ 2528 Mayor's posters

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  5. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  6. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  7. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  8. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  9. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

随机推荐

  1. python whl包的安装

    1.首先安装PIP. https://pip.pypa.io/en/latest/installing.html#install-pip python get-pip.py 2.设置好环境变量: C: ...

  2. vue-router导航守卫

    导航守卫 译者注 “导航”表示路由正在发生改变. 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件 ...

  3. BZOJ4358:permu

    浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...

  4. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  5. UOJ #55 & 洛谷 P3920 紫荆花之恋 —— 动态点分治+替罪羊树

    题目:http://uoj.ac/problem/55 https://www.luogu.org/problemnew/show/P3920 参考博客:https://www.cnblogs.com ...

  6. k近邻算法C++二维情况下的实现

    k近邻算法C++二维实现 这是一个k近邻算法的二维实现(即K=2的情况). #include <cstdio> #include <cstring> #include < ...

  7. 编译cef 2526

    fetch --nohooks chromium cd /path/to/chromium/src# git checkout -b 51.0.2704.103 refs/tags/51.0.2704 ...

  8. svn-clearup 报错的处理(Cleanup failed to process the following paths...)

    在使用 svn 客户端执行操作失败后,执行 Clean up 操作也报错:Cleanup failed to process the following paths... ,一直不知道是什么原因.通常 ...

  9. AWS + Stunnel + Squid ***

    [需求] 第一,能***. 第二,在企业网络要能突破端口限制. [原理] 利用AWS提供的一年免费EC2服务,搭建一台自己的VPS,在VPS中利用Stunnel与本机建立加密连接,将本地http请求通 ...

  10. PostgreSQL 监控磁盘使用

    监控磁盘使用 1. 判断磁盘用量 每个表都有一个主要的堆磁盘文件,大多数数据都存储在其中.如果一个表有着可能会很宽(尺寸大)的列, 则另外还有一个TOAST文件与这个表相关联, 它用于存储因为太宽而不 ...