Posters TopCoder - 1684
分析
首先我们不难想到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的更多相关文章
- POJ2528Mayor's posters[线段树 离散化]
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59683 Accepted: 17296 ...
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- POJ 2528 Mayor's posters
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
随机推荐
- OpenCV - opencv3 图像处理 之 图像缩放( python与c++实现 )
转自:https://www.cnblogs.com/dyufei/p/8205121.html 一. 主要函数介绍 1) 图像大小变换 cvResize () 原型: voidcvResize(co ...
- HDU - 5306: Gorgeous Sequence (势能线段树)
There is a sequence aa of length nn. We use aiai to denote the ii-th element in this sequence. You s ...
- python爬虫框架Pyspider初次接触
pyspider网站地址:http://docs.pyspider.org/en/latest/.文档比较好,安装起来也非常方便.既然是基于python的框架,那么首先得安装python.微软出的一款 ...
- Real-Time Rendering (2) - 变换和矩阵(Transforms and Matrics)
http://blog.csdn.net/silangquan/article/details/9970673 提要 在图形的计算中,比如旋转.缩放.平移.投影等操作,矩阵都扮演着极其重要的角色,它是 ...
- python函数之sorted与sort
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...
- VC 6.0下载 VC 6.0英文版下载 Visual C++ 6.0 英文企业版 集成SP6完美版(最新更新地址,百度网盘)
下载地址1:Visual.C++.6.EN 下载地址2:Visual.C++.6.EN 更新下载地址可用(百度网盘)Visual.C++.6.EN 转载请注明出处,有技术问题,欢迎互相交流,或者留言.
- bzoj 2982 combination——lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 明明是lucas定理裸题…… 非常需要注意C( )里 if ( n<m ) r ...
- hl7中V2版本的ACK消息的构造
hl7 v2的ack消息即应答消息构造时有几个注意的地方. 首先,我们看下2个ack的例子: Send: MSH|^~\&|NIST_SENDER^^|NIST^^|NIST_RECEIVER ...
- mysql的备份恢复等操作
备份数据库 shell> mysqldump -h host -u root -p dbname >dbname_backup.sql 恢复数据库 shell> mysqladmin ...
- 最小LINUX系统下U盘的挂载及卸载
U盘挂载命令U盘插入的时候会显示启动信息,启动信息中sda: sda1指U盘的设备名为sda1dev设备目录下有一个sda1设备文件,此设备文件就是我们插入的U盘,我们将这个设备文件挂载到Linux系 ...