USACO 6.2 Packing Rectangles

Packing Rectangles
IOI 95USACO 6.2 Packing Rectangles 
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 described by 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 ————————————————————————————————————————————————题解
我们枚举每一种情况
1.将编号为1 2 3 4的全排列
2.将排列后的1情况的每一个矩形枚举转还是不转
将1、2做完之后手动模拟6种情况即可
 /*
ID: ivorysi
LANG: C++
PROG: packrec
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int n,area=inf;
vector<pii> v;
void record(pii t) {
if(t.fi>t.se) swap(t.fi,t.se);
if(t.fi*t.se==area) v.push_back(t);
else if(t.fi*t.se<area) {
v.clear();
area=t.fi*t.se;
v.push_back(t);
}
}
struct data {
int l,r;
}squ[],rec[];
inline void rotate(data &a) {
swap(a.l,a.r);
}
bool used[];
void calc() {
pii w;
//fi是竖边,se是横边
//case 1
w.fi=;
siji(i,,) w.fi=max(w.fi,rec[i].l);
siji(i,,) w.se+=rec[i].r;
record(w);
//case 2
int temp2=;
siji(i,,) temp2=max(rec[i].l,temp2);
w.fi=rec[].l+temp2;
w.se=;
siji(i,,) w.se+=rec[i].r;
w.se=max(w.se,rec[].r);
record(w);
//case 3
w.fi=max(rec[].l,rec[].l)+rec[].l;
w.fi=max(w.fi,rec[].l);
w.se=max(rec[].r,rec[].r+rec[].r)+rec[].r;
record(w);
//case 4,5
w.fi=max(rec[].l,rec[].l);
w.fi=max(w.fi,rec[].l+rec[].l);
w.se=rec[].r+rec[].r;
w.se+=max(rec[].r,rec[].r);
record(w);
//case 6
// 1 2
// 3 4
w.fi=max(rec[].l+rec[].l,rec[].l+rec[].l);
w.se=rec[].r+rec[].r;
// 1与2
if(rec[].l+rec[].l>rec[].l) w.se=max(w.se,rec[].r+rec[].r);
// 2与3
if(rec[].l>rec[].l) w.se=max(w.se,rec[].r+rec[].r);
// 1与4
if(rec[].l>rec[].l) w.se=max(w.se,rec[].r+rec[].r);
// 1 或 2 特别长
w.se=max(w.se,rec[].r);
w.se=max(w.se,rec[].r);
record(w);
}
void dfs1(int k) {
if(k>) {
calc();
return;
}
dfs1(k+);//不转这个
rotate(rec[k]);
dfs1(k+);//转这个
rotate(rec[k]);
}
void dfs(int k) {
if(k>) {
dfs1();
}
siji(i,,) {
if(!used[i]) {
rec[k]=squ[i];
used[i]=;
dfs(k+);
used[i]=;
}
}
}
void solve() {
siji(i,,) scanf("%d%d",&squ[i].l,&squ[i].r);
dfs();
sort(v.begin(),v.end());
vector<pii>::iterator it=unique(v.begin(),v.end());
v.erase(it,v.end());
printf("%d\n",area);
siji(i,,v.size()-) {
printf("%d %d\n",v[i].fi,v[i].se);
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("packrec.in","r",stdin);
freopen("packrec.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
 
上一篇:shell编程-1到100的求和与冒泡排序


下一篇:python基础之五大标准数据类型