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.

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 ;
}
 

USACO 6.2 Packing Rectangles的更多相关文章

  1. Section 1.4 Packing Rectangles

    本来是USACO Training的1.4.1的,但是介于今早过了食物链想起了这道题实在是太怨念了,翻出自己写的AC程序居然有5KB!! 思路很简单,枚举,而且就图中的六种情况.但是第六种变化状况太多 ...

  2. USACO1.4.1 Packing Rectangles

    //毕竟我不是dd牛,USACO的题解也不可能一句话带过的…… 题目链接:http://cerberus.delos.com:790/usacoprob2?a=pWvHFwGsTb2&S=pa ...

  3. [vijos P1531] 食物链

    做出的第一道NOI题目?(噗,还是看题解才会的…按某篇解题说的,这题就比我年轻四岁…T T 做的第一道IOI题目是USACO上的Packing Rectangles...这题比我还老!)对我等弱渣来说 ...

  4. USACO chapter1

    几天时间就把USACO chapter1重新做了一遍,发现了自己以前许多的不足.蒽,现在的程序明显比以前干净很多,而且效率也提高了许多.继续努力吧,好好的提高自己.这一章主要还是基本功的训练,没多少的 ...

  5. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  6. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  7. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  8. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  9. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

随机推荐

  1. Java基础-字符串(String)常用方法

    Java基础-字符串(String)常用方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.java的API概念 Java的API(API:Application(应用) Pr ...

  2. Java基础-包(package)的声明与访问

    Java基础-包(package)的声明与访问 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.包的概念 Java中的包,其实就是我们电脑系统中的文件夹,包里存放的是程序员生成的 ...

  3. 《剑指offer》面试题39 二叉树的深度(java)

    摘要: 今天翻到了<剑指offer>面试题39,题目二中的解法二是在函数的参数列表中通过指针的方式进行传值,而java是没有指针的,所以函数要进行改造.然而我翻了下别人的java版本(我就 ...

  4. day6 方法

    1.方法是一段可重复调用的代码段,今天学习的方法可以由主方法直接调用,所以加入public static关键字修饰. 2.方法的重载为方法名相同,参数的类型或个数不同.

  5. mongodb 跟踪SQL语句及慢查询收集

    有个需求:跟踪mongodb的SQL语句及慢查询收集 第一步:通过mongodb自带函数可以查看在一段时间内DML语句的运行次数. 在bin目录下面运行  ./mongostat -port 端口号  ...

  6. kibana多台服务部署

    nohup /usr/share/kibana/bin/kibana -c /etc/kibana/kibana5602.yml & cp kibana.yml kibana5602.yml ...

  7. javascript的未知尺寸图片保持比例水平垂直居中函数

    JavaScript的图片在容器内水平垂直居中的函数,利用图片加载获取图片大小,使之在父节点内水平垂直居中 展示方式有两种: 1.当参数keepImageFull为true:保持图片比例,使图片可完整 ...

  8. PIE的使用

    实际上是指的是一个名为pie的htc文件,即pie.htc,使用CSS的behavior行为,可以调用此文件,然后让IE也能实现一些常见的 CSS3效果,如圆角(border-radius),盒阴影( ...

  9. HDU 4500 小Q系列故事——屌丝的逆袭

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4500 解题报告:简单题,数据范围不大,直接暴力每个点,然后再比较出得分最大的点的位置和分数. #inc ...

  10. 2016.5.14——leetcode-HappyNumber,House Robber

    leetcode:HappyNumber,House Robber 1.Happy Number 这个题中收获2点: 1.拿到题以后考虑特殊情况,代码中考虑1和4,或者说<6的情况,动手算下.( ...