USACO 6.2 Packing Rectangles
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的更多相关文章
- Section 1.4 Packing Rectangles
本来是USACO Training的1.4.1的,但是介于今早过了食物链想起了这道题实在是太怨念了,翻出自己写的AC程序居然有5KB!! 思路很简单,枚举,而且就图中的六种情况.但是第六种变化状况太多 ...
- USACO1.4.1 Packing Rectangles
//毕竟我不是dd牛,USACO的题解也不可能一句话带过的…… 题目链接:http://cerberus.delos.com:790/usacoprob2?a=pWvHFwGsTb2&S=pa ...
- [vijos P1531] 食物链
做出的第一道NOI题目?(噗,还是看题解才会的…按某篇解题说的,这题就比我年轻四岁…T T 做的第一道IOI题目是USACO上的Packing Rectangles...这题比我还老!)对我等弱渣来说 ...
- USACO chapter1
几天时间就把USACO chapter1重新做了一遍,发现了自己以前许多的不足.蒽,现在的程序明显比以前干净很多,而且效率也提高了许多.继续努力吧,好好的提高自己.这一章主要还是基本功的训练,没多少的 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- 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 ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
随机推荐
- bzoj千题计划135:bzoj1066: [SCOI2007]蜥蜴
http://www.lydsy.com/JudgeOnline/problem.php?id=1066 每个柱子拆成两个点 i<<1,i<<1|1,之间连流量为高度的边 如果 ...
- Java并发编程原理与实战二十五:ThreadLocal线程局部变量的使用和原理
1.什么是ThreadLocal ThreadLocal顾名思义是线程局部变量.这种变量和普通的变量不同,这种变量在每个线程中通过get和set方法访问, 每个线程有自己独立的变量副本.线程局部变量不 ...
- CSS只改变背景透明度,不改变子元素透明度
一般情况下,我们可以使用css的opcity属性改变某个元素的透明度,但是其元素下的子元素的透明度也会被改变,即使对子元素重新定义也没有用,例如: <div style="opacit ...
- 小玩意1-实时获取IE浏览器输入框URL地址
主要参考http://www.cnblogs.com/scrat/archive/2012/09/12/2682626.html 主要思路如下: 通过 FindWindow() FindWindowE ...
- spring boot获取前端参数四种方法
一:直接参数绑定 @RequestMapping("/hello") @ResponseBody public String hello(String para) { // par ...
- Eclipse中如何改变主题
童鞋们, eclipse主题太丑?想设置护眼的主题? 看看这些主题: 请移驾: Eclipse Color Themes 怎么设设置? 1. 打开”eclipse marketplace“, 如下图: ...
- dubbox ExceptionMapper Filter request response 数据获取 数据传递
dubbx虽然是基于jboss的resteasy实现restfull,但是对resteasy原生的配置却不支持(可能是考虑到dubbo本事的设计模式及实现难度,但是和大部分framework的设计风格 ...
- Python标准库笔记(8) — pprint模块
struct模块提供了用于在字节字符串和Python原生数据类型之间转换函数,比如数字和字符串. Python版本: 2.x & 3.x 该模块作用是完成Python数值和C语言结构体的Pyt ...
- ajax代码示例
function loadXMLDoc(idName,url,sendOut) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, ...
- 神经网络中的激活函数tanh sigmoid RELU softplus softmatx
所谓激活函数,就是在神经网络的神经元上运行的函数,负责将神经元的输入映射到输出端.常见的激活函数包括Sigmoid.TanHyperbolic(tanh).ReLu. softplus以及softma ...