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. linux下项目上线配置nginx+tomcat

    nginx.conf server { listen 80; server_name www.examples.com; client_max_body_size 300m; #charset koi ...

  2. 15 Most Read Data Science Articles in 2015. So far …

    15 Most Read Data Science Articles in 2015. So far … We've compiled the latest set of "most rea ...

  3. Matlab——GUI初涉

    Matlab——GUI初涉 MATLAB GUI教学视频0:GUI中的基本操作—在线播放—优酷网,视频高清在线观看http://v.youku.com/v_show/id_XMjM2Mjk0MjM2. ...

  4. 《大型网站SEO优化实践》学习分享

    本文主要内容源自2013年阿里技术嘉年华中阿里巴巴周文君分享<大型网站SEO优化实践>.学习过后,受益匪浅,特作笔记,经常回顾吸收学习. 大型网站SEO的特点&优势&挑战 ...

  5. 生死相依:说说JQuery中die()、live()详解[翻译]

    一个web前端工程师,应该知道jquery的.live()函数,知道它是做什么用的,但是不知它是怎么样工作的,使用起来也是不得得心应手的,甚至也没听说过.die()(去掉bind事件).即使你能熟悉这 ...

  6. 训练赛第二场G题 ZOJ 2343

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2343 解题报告:首先我假设最后的正确的结果是a[1] , a[2 ...

  7. 【leetcode 简单】 第六十九题 删除链表中的节点

    请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 - ...

  8. Understanding the Space Used by ZFS -- (转)

    Understanding the Space Used by ZFS By Brian Leonard on Sep 28, 2010 Until recently, I've been confu ...

  9. 2016.5.19——vector型的输入输出

    vector型的输入输出 在上节2015.5.18——leetcode:Majority Element中纠结vector的动态输入输出问题,但是发现vector传参型的不可以动态输入输出,但是vec ...

  10. 2017-2018-2 20179205《网络攻防技术与实践》Windows攻击实验

    Windows攻击实验 实验描述: 使用Metaspoit攻击MS08-067,提交正确得到远程shell过程的截图(不少于五张). MS08-067漏洞介绍   MS08-067漏洞的全称为&quo ...