码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 6.2 Packing Rectangles

时间:2017-04-03 19:34:41      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:lines   without   using   basic   and   div   esc   6.2   std   

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种情况即可
  1 /*
  2 ID: ivorysi
  3 LANG: C++
  4 PROG: packrec
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <queue>
 10 #include <set>
 11 #include <vector>
 12 #include <algorithm>
 13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
 14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
 15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
 16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
 17 #define inf 0x5f5f5f5f
 18 #define ivorysi
 19 #define mo 97797977
 20 #define hash 974711
 21 #define base 47
 22 #define fi first
 23 #define se second
 24 #define pii pair<int,int>
 25 #define esp 1e-8
 26 typedef long long ll;
 27 using namespace std;
 28 int n,area=inf;
 29 vector<pii> v;
 30 void record(pii t) {
 31     if(t.fi>t.se) swap(t.fi,t.se);
 32     if(t.fi*t.se==area) v.push_back(t);
 33     else if(t.fi*t.se<area) {
 34         v.clear();
 35         area=t.fi*t.se;
 36         v.push_back(t);
 37     }
 38 } 
 39 struct data {
 40     int l,r;
 41 }squ[5],rec[5];
 42 inline void rotate(data &a) {
 43     swap(a.l,a.r);
 44 }
 45 bool used[5];
 46 void calc() {
 47     pii w;
 48     //fi是竖边,se是横边
 49     //case 1
 50     w.fi=0;
 51     siji(i,1,4) w.fi=max(w.fi,rec[i].l);
 52     siji(i,1,4) w.se+=rec[i].r;
 53     record(w);
 54     //case 2
 55     int temp2=0;
 56     siji(i,1,3) temp2=max(rec[i].l,temp2);
 57     w.fi=rec[4].l+temp2;
 58     w.se=0;
 59     siji(i,1,3) w.se+=rec[i].r;
 60     w.se=max(w.se,rec[4].r);
 61     record(w);
 62     //case 3
 63     w.fi=max(rec[1].l,rec[2].l)+rec[3].l;
 64     w.fi=max(w.fi,rec[4].l);
 65     w.se=max(rec[3].r,rec[1].r+rec[2].r)+rec[4].r;
 66     record(w);
 67     //case 4,5
 68     w.fi=max(rec[1].l,rec[2].l);
 69     w.fi=max(w.fi,rec[3].l+rec[4].l);
 70     w.se=rec[1].r+rec[2].r;
 71     w.se+=max(rec[3].r,rec[4].r);
 72     record(w);
 73     //case 6
 74     // 1 2
 75     // 3 4
 76     w.fi=max(rec[1].l+rec[3].l,rec[2].l+rec[4].l);
 77     w.se=rec[3].r+rec[4].r;
 78     // 1与2
 79     if(rec[1].l+rec[3].l>rec[4].l) w.se=max(w.se,rec[1].r+rec[2].r);
 80     // 2与3
 81     if(rec[3].l>rec[4].l) w.se=max(w.se,rec[2].r+rec[3].r);
 82     // 1与4
 83     if(rec[4].l>rec[3].l) w.se=max(w.se,rec[1].r+rec[4].r);
 84     // 1 或 2 特别长
 85     w.se=max(w.se,rec[1].r);
 86     w.se=max(w.se,rec[2].r);
 87     record(w); 
 88 }
 89 void dfs1(int k) {
 90     if(k>4) {
 91         calc();
 92         return;
 93     }
 94     dfs1(k+1);//不转这个
 95     rotate(rec[k]);
 96     dfs1(k+1);//转这个
 97     rotate(rec[k]);
 98 }
 99 void dfs(int k) {
100     if(k>4) {
101         dfs1(1);
102     }
103     siji(i,1,4) {
104         if(!used[i]) {
105             rec[k]=squ[i];
106             used[i]=1;
107             dfs(k+1);
108             used[i]=0;
109         }
110     }
111 }
112 void solve() {
113     siji(i,1,4) scanf("%d%d",&squ[i].l,&squ[i].r);
114     dfs(1);
115     sort(v.begin(),v.end());
116     vector<pii>::iterator it=unique(v.begin(),v.end());
117     v.erase(it,v.end());
118     printf("%d\n",area);
119     siji(i,0,v.size()-1) {
120         printf("%d %d\n",v[i].fi,v[i].se);
121     }
122 }
123 int main(int argc, char const *argv[])
124 {
125 #ifdef ivorysi
126     freopen("packrec.in","r",stdin);
127     freopen("packrec.out","w",stdout);
128 #else
129     freopen("f1.in","r",stdin);
130 #endif
131     solve();
132     return 0;
133 }

 

 

USACO 6.2 Packing Rectangles

标签:lines   without   using   basic   and   div   esc   6.2   std   

原文地址:http://www.cnblogs.com/ivorysi/p/6662962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!