您的位置:首页 > 其它

USACO 1.4 Packing Rectangles(模拟)

2012-10-24 21:26 218 查看
这个题,不简单啊,虽说是模拟,但是需要注意的情况有很多。开始看错了题,理解正确题意之后,依旧写了很久,错了N次后,终于到了最后一组数据,终于给水过去了,最后一种情况我是分两种情况讨论的,也不知写的对不对。。。

/*
ID: cuizhe
LANG: C++
TASK: packrec
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <algorithm>
using namespace std;
int px[5],py[5],k[5];
int o[100001],ans,num,sum,temp;
void cl()
{

if(ans > sum*temp)
{
ans = sum*temp;
memset(o,0,sizeof(o));
o[min(sum,temp)] = 1;
}
else if(ans == sum*temp)
{
o[min(sum,temp)] = 1;
}
}
void dfs(int x)
{
int i,j;
int mx[5],my[5];
if(x > 4)
{
for(i = 0; i < 1<<4; i ++)
{
for(j = 0; j <= 3; j ++)
{
if(i&(1<<j))
{
mx[j] = px[k[j+1]];
my[j] = py[k[j+1]];
}
else
{
my[j] = px[k[j+1]];
mx[j] = py[k[j+1]];
}
}
sum = my[0]+my[1]+my[2]+my[3];//方法1
temp = mx[0];
for(j = 1; j <= 3; j ++)
{
temp = max(mx[j],temp);
}
cl();
sum = max(my[0]+my[1]+my[2],my[3]);//方法2
temp = mx[0];
for(j = 1; j <= 2; j ++)
{
temp = max(mx[j],temp);
}
temp += mx[3];
cl();
sum = max(my[0]+my[1],my[3]);//方法3
sum += my[2];
temp = mx[2];
temp = max(temp,mx[0]+mx[3]);
temp = max(temp,mx[1]+mx[3]);
cl();
sum = my[0]+max(my[1],my[2])+my[3];//方法4
temp = mx[0];
temp = max(mx[1]+mx[2],temp);
temp = max(temp,mx[3]);
cl();
sum = my[2]+max(my[0],my[1])+my[3];//方法5
temp = mx[2];
temp = max(mx[0]+mx[1],temp);
temp = max(temp,mx[3]);
cl();
sum =  max(mx[0],mx[1])+max(mx[2],mx[3]);//方法6分2种情况
temp = max(my[0]+my[1],my[2]+my[3]);
cl();
if(my[0]+my[1] >= my[2]+my[3])
{
sum = my[0]+my[1];
if(my[3] > my[1])
{
temp = max(mx[3]+max(mx[0],mx[1]),mx[2]+mx[0]);
cl();
}
else if(my[2] > my[0])
{
temp = max(mx[2]+max(mx[0],mx[1]),mx[3]+mx[1]);
cl();
}
}
}
}
for(i = 1; i <= 4; i ++)
{
if(!k[i])
{
k[i] = x;
dfs(x+1);
k[i] = 0;
}
}

}
int main()
{
int i,t;
freopen("packrec.in","r",stdin);
freopen("packrec.out","w",stdout);
for(i = 1; i <= 4; i ++)
{
scanf("%d%d",&px[i],&py[i]);
if(px[i] > py[i])
{
t = px[i];
px[i] = py[i];
py[i] = t;
}
}
ans = 10000000;
dfs(1);
printf("%d\n",ans);
for(i = 1; i <= 100000; i ++)
{
if(o[i])
printf("%d %d\n",i,ans/i);
}
return 0;
}
/*
4 5
4 5
4 5
1 16
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: