USACO Packing Rectangles
2014-02-02 09:06
218 查看
Packing Rectangles
IOI 95
The six basic layouts of four rectangles
Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. By smallest rectangle, we mean the one with the smallest area.
All four rectangles should have their sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows six ways to fit four rectangles together. These six are the only possible basic layouts,
since any other layout can be obtained from a basic layout by rotation or reflection. Rectangles may be rotated 90 degrees during packing.
There may exist several different enclosing rectangles fulfilling the requirements, all with the same area. You must produce all such enclosing rectangles.
by two numbers p and q with p<=q. These lines must be sorted in ascending order of p, and must all be different.
Submission file Name:
USACO Gateway | Comment or Question
/*
ID: qhn9992
PROG: packrec
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> pII;
int rect[5][2];
int ANS[500][2],tot,MIN=INF;
int a1,a2,a3,a4,b1,b2,b3,b4;
void update(int weight,int height)
{
int area=weight*height;
int mi=min(weight,height);
int mx=max(weight,height);
if(area>MIN) return ;
else if(area<MIN)
{
tot=1;
MIN=area;
ANS[0][0]=mi;ANS[0][1]=mx;
return ;
}
else
{
for(int i=0;i<tot;i++)
{
if(ANS[i][0]==mi&&ANS[i][1]==mx) return ;
}
ANS[tot][0]=mi;ANS[tot][1]=mx;
tot++;
}
}
void solve()
{
int wide,high;
int w1=rect[a1][b1],w2=rect[a2][b2],w3=rect[a3][b3],w4=rect[a4][b4];
int h1=rect[a1][1-b1],h2=rect[a2][1-b2],h3=rect[a3][1-b3],h4=rect[a4][1-b4];
///cas1
wide=w1+w2+w3+w4;
high=max(max(h1,h2),max(h3,h4));
update(wide,high);
///cas2
wide=max(w4,w1+w2+w3);
high=max(h1,max(h2,h3))+h4;
update(wide,high);
///cas3
wide=w4+max(w1+w2,w3);
high=max(h4,max(h1,h2)+h3);
update(wide,high);
///cas4/5
wide=w1+w4+max(w2,w3);
high=max(h1,max(h2+h3,h4));
update(wide,high);
///cas6
if(h2>=h4&&(w2<w1||w3<w4))
wide=max(w2+w4,w1+w3);
else wide=max(w1,w2)+max(w3,w4);
high=max(h1+h2,h3+h4);
update(wide,high);
}
int main()
{
freopen("packrec.in","r",stdin);
freopen("packrec.out","w",stdout);
for(int i=0;i<4;i++)
{
int a,b;
scanf("%d%d",&a,&b);
rect[i][0]=a;rect[i][1]=b;
}
for(a1=0;a1<4;a1++)
{
for(a2=0;a2<4;a2++)
{
if(a1==a2) continue;
for(a3=0;a3<4;a3++)
{
if(a3==a2||a3==a1) continue;
a4=6-a1-a2-a3;
for(b1=0;b1<2;b1++)
for(b2=0;b2<2;b2++)
for(b3=0;b3<2;b3++)
for(b4=0;b4<2;b4++)
solve();
}
}
}
vector<pII> ans;
for(int i=0;i<tot;i++)
{
int a=ANS[i][0],b=ANS[i][1];
ans.push_back(make_pair(a,b));
}
sort(ans.begin(),ans.end());
printf("%d\n",MIN);
for(int i=0;i<tot;i++)
{
printf("%d %d\n",ans[i].first,ans[i].second);
}
return 0;
}
IOI 95
The six basic layouts of four rectangles
Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. By smallest rectangle, we mean the one with the smallest area.
All four rectangles should have their sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows six ways to fit four rectangles together. These six are the only possible basic layouts,
since any other layout can be obtained from a basic layout by rotation or reflection. Rectangles may be rotated 90 degrees during packing.
There may exist several different enclosing rectangles fulfilling the requirements, all with the same area. You must produce all such enclosing rectangles.
PROGRAM NAME: packrec
INPUT FORMAT
Four lines, each containing two positive space-separated integers that represent the lengths of a rectangle's two sides. Each side of a rectangle is at least 1 and at most 50.SAMPLE INPUT (file packrec.in)
1 2 2 3 3 4 4 5
OUTPUT FORMAT
The output file contains one line more than the number of solutions. The first line contains a single integer: the minimum area of the enclosing rectangles. Each of the following lines contains one solution describedby two numbers p and q with p<=q. These lines must be sorted in ascending order of p, and must all be different.
SAMPLE OUTPUT (file packrec.out)
40 4 10 5 8
Submission file Name:
USACO Gateway | Comment or Question
/*
ID: qhn9992
PROG: packrec
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> pII;
int rect[5][2];
int ANS[500][2],tot,MIN=INF;
int a1,a2,a3,a4,b1,b2,b3,b4;
void update(int weight,int height)
{
int area=weight*height;
int mi=min(weight,height);
int mx=max(weight,height);
if(area>MIN) return ;
else if(area<MIN)
{
tot=1;
MIN=area;
ANS[0][0]=mi;ANS[0][1]=mx;
return ;
}
else
{
for(int i=0;i<tot;i++)
{
if(ANS[i][0]==mi&&ANS[i][1]==mx) return ;
}
ANS[tot][0]=mi;ANS[tot][1]=mx;
tot++;
}
}
void solve()
{
int wide,high;
int w1=rect[a1][b1],w2=rect[a2][b2],w3=rect[a3][b3],w4=rect[a4][b4];
int h1=rect[a1][1-b1],h2=rect[a2][1-b2],h3=rect[a3][1-b3],h4=rect[a4][1-b4];
///cas1
wide=w1+w2+w3+w4;
high=max(max(h1,h2),max(h3,h4));
update(wide,high);
///cas2
wide=max(w4,w1+w2+w3);
high=max(h1,max(h2,h3))+h4;
update(wide,high);
///cas3
wide=w4+max(w1+w2,w3);
high=max(h4,max(h1,h2)+h3);
update(wide,high);
///cas4/5
wide=w1+w4+max(w2,w3);
high=max(h1,max(h2+h3,h4));
update(wide,high);
///cas6
if(h2>=h4&&(w2<w1||w3<w4))
wide=max(w2+w4,w1+w3);
else wide=max(w1,w2)+max(w3,w4);
high=max(h1+h2,h3+h4);
update(wide,high);
}
int main()
{
freopen("packrec.in","r",stdin);
freopen("packrec.out","w",stdout);
for(int i=0;i<4;i++)
{
int a,b;
scanf("%d%d",&a,&b);
rect[i][0]=a;rect[i][1]=b;
}
for(a1=0;a1<4;a1++)
{
for(a2=0;a2<4;a2++)
{
if(a1==a2) continue;
for(a3=0;a3<4;a3++)
{
if(a3==a2||a3==a1) continue;
a4=6-a1-a2-a3;
for(b1=0;b1<2;b1++)
for(b2=0;b2<2;b2++)
for(b3=0;b3<2;b3++)
for(b4=0;b4<2;b4++)
solve();
}
}
}
vector<pII> ans;
for(int i=0;i<tot;i++)
{
int a=ANS[i][0],b=ANS[i][1];
ans.push_back(make_pair(a,b));
}
sort(ans.begin(),ans.end());
printf("%d\n",MIN);
for(int i=0;i<tot;i++)
{
printf("%d %d\n",ans[i].first,ans[i].second);
}
return 0;
}
相关文章推荐
- 火天大有 (易經大意 韓長庚)
- python是最适合初学者的语言
- 推荐算法之协同过滤实战
- 地山謙 (易經大意 韓長庚)
- JAVASE 面向对象 多态 单类设计模式 内部类 匿名内部类
- LeetCode 049 Anagrams
- LeetCode 000 前言
- ActionContext.getContext()详解
- Oracle数据库中的坏块问题处理
- 雷地豫 (易經大意 韓長庚)
- java获取客户端IP地址以及mac地址
- freemarker 整理手册
- Freemarker 中文API手册
- main函数的参数
- JS获取本地IP、MAC地址
- 李思思_百度百科
- Ch8.8: 8 queen problem
- 地澤臨 (易經大意 韓長庚)
- 怎样把IE设置成默认浏览器
- Oracle创建表空间、创建用户、授予权限、锁定、解锁以及删除用户等