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 ...
随机推荐
- 终于把5GB的Cygwin安装完成了
From:http://www.cnblogs.com/killerlegend/p/3904478.html Author:KillerLegend Date:2014.8.11 首先,只想说一句, ...
- Spring在Web容器启动时执行初始化方法
需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法 ...
- 51 nod 1046 A^B Mod C
1046 A^B Mod C 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^ ...
- Composer学习之————Ubuntu14.04下安装Composer
下载Composer: curl -sS https://getcomposer.org/installer | php 安装Composer: /usr/bin/php composer.phar ...
- Django 2.0.1 官方文档翻译: 高级教程:如何编写可重用的app (page 13)
高级教程:如何编写可重用的app (page 13) 本节教程上接第七部分(Page 12).我们会把我们的 web-poll应用转换成一个独立的python包,你可以在新的项目中重用或者把它分享给其 ...
- Java并发编程原理与实战十三:JDK提供的原子类原理与使用
原子更新基本类型 原子更新数组 原子更新抽象类型 原子更新字段 原子更新基本类型: package com.roocon.thread.t8; import java.util.concurren ...
- python 常用对linux系统文件及目录的操作
目录 1.取得当前目录——os.getcwd() >>> import os >>> s=os.getcwd()#获得当前运行脚本所在目录 >>> ...
- Elasticsearch技术解析与实战(三)文档的聚合
1.计算每个tag下的商品数量 PUT /database/_mapping/product { "properties": { "tags": { " ...
- 你知道吗?undefined 与 null 的区别
大多数计算机语言,有且仅有一个表示"无"的值,比如,C语言的NULL,Java语言的null,Python语言的none,Ruby语言的nil. 有点奇怪的是,JavaScript ...
- for-in 和 for
本文地址:http://www.cnblogs.com/veinyin/p/8745845.html 1 for for ( var i = 0 ; i < len ; i++ ) for 是 ...