[USACO 6.2.2] Packing Rectangles

题目大意

  给出4个矩形,求一个面积最小的矩形能够容纳着4个矩形,4个矩形不能互相叠起来.

题解

  看USACO的图,它并没有给出全部情况.

  还有两种情况.比较好想.这里不一一说明.

  其实这题考察大家的缜密的思维以及是否拥有良好的代码习惯(因为有时候敲起来会很乱).

代码

/*
TASK:packrec
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Matrix
{
    int h, w;

    bool operator < (const Matrix &b) const
    {
        return h < b.h || h == b.h && w < b.w;
    }
}ans[1024], minmat[4], a[4];

int anslen;
bool v[4];

void judge()
{
    if (ans[anslen].h > ans[anslen].w) swap(ans[anslen].h, ans[anslen].w);
    if (ans[anslen].h * ans[anslen].w < ans[0].h * ans[0].w)
    {
        ans[0] = ans[anslen];
        anslen = 1;
        return;
    }
    if (ans[anslen].h * ans[anslen].w == ans[0].h * ans[0].w) anslen++;
}

void dfs(int s)
{
    if (s == 4)
    {
        //1
        ans[anslen].h = max(minmat[0].h, max(minmat[1].h, max(minmat[2].h, minmat[3].h)));
        ans[anslen].w = minmat[0].w + minmat[1].w + minmat[2].w + minmat[3].w;
        judge();
        //2
        ans[anslen].h = max(minmat[0].h, max(minmat[1].h, minmat[2].h)) + minmat[3].h;
        ans[anslen].w = max(minmat[3].w, minmat[0].w + minmat[1].w + minmat[2].w);
        judge();
        //3
        ans[anslen].h = max(minmat[3].h, max(minmat[0].h, minmat[1].h) + minmat[2].h);
        ans[anslen].w = max(minmat[0].w + minmat[1].w, minmat[2].w) + minmat[3].w;
        judge();
        //4
        ans[anslen].h = max(minmat[0].h, max(minmat[1].h + minmat[2].h, minmat[3].h));
        ans[anslen].w = minmat[0].w + max(minmat[1].w, minmat[2].w) + minmat[3].w;
        judge();
        //5
        ans[anslen].h = max(minmat[0].h, minmat[1].h) + minmat[2].h + minmat[3].h;
        ans[anslen].w = max(minmat[0].w, minmat[1].w) + minmat[2].w + minmat[3].w;
        judge();
        //6
        if (minmat[0].w <= minmat[1].w && minmat[1].h <= minmat[3].h)
        {
            ans[anslen].h = max(minmat[0].h + minmat[1].h, minmat[2].h + minmat[3].h);
            ans[anslen].w = max(minmat[1].w + minmat[3].w, minmat[0].w + minmat[2].w);
            judge();
        }
    }

    for (int i = 0; i < 4; ++i)
        if (v[i])
        {
            v[i] = false;
            minmat[s].h = a[i].h;
            minmat[s].w = a[i].w;
            dfs(s+1);
            swap(minmat[s].h, minmat[s].w);
            dfs(s+1);
            v[i] = true;
        }
}

int main()
{
    freopen("packrec.in", "r", stdin);
    freopen("packrec.out", "w", stdout);
    for (int i = 0; i < 4; ++i) scanf("%d %d", &a[i].h, &a[i].w);
    memset(v, true, sizeof(v));
    anslen = 0;
    dfs(0);
    sort(ans, ans + anslen);
    printf("%d\n", ans[0].h * ans[0].w);
    printf("%d %d\n", ans[0].h, ans[0].w);
    for (int i = 1; i < anslen; ++i)
        if (ans[i].h != ans[i - 1].h || ans[i].w != ans[i - 1].w)
            printf("%d %d\n", ans[i].h, ans[i].w);
    return 0;
}
时间: 2024-08-27 11:59:38

[USACO 6.2.2] Packing Rectangles的相关文章

USACO 6.2 Packing Rectangles

Packing RectanglesIOI 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

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

【USACO 1.3.4】牛式

[題目描述 ] 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ---------- * * * * * * ---------- * * * * 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [格式] INPUT FORMAT: (f

USACO Chapter 1 Section 1.1

USACO的题解和翻译已经很多了... 我只是把自己刷的代码保存一下. 1.PROB Your Ride Is Here 1 /* 2 ID:xiekeyi1 3 PROG:ride 4 LANG:C++ 5 */ 6 7 #include<bits/stdc++.h> 8 using namespace std ; 9 10 int main() 11 { 12 freopen("ride.in","r",stdin); 13 freopen(&quo

USACO Your Ride Is Here

[USACO]Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do,

usaco月赛,2017.1总结

T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现在给出奶牛个数,最多用时,每头奶牛的跳舞时间.求舞台最小为多大. 思路:本来写了个程序以为这道题很简单,刚开始排一下序然后就行了,结果交了以后发现只过了五组,然后才发现这道题不能改变顺序(所以说为什么我改变顺序了还是能过五组,usaco的数据也好水......),所以说我想到了堆,然后就用堆写了一下

插入排序的优化【不靠谱地讲可以优化到O(nlogn)】 USACO 丑数

首先我们先介绍一下普通的插排,就是我们现在一般写的那种,效率是O(n^2)的. 普通的插排基于的思想就是找位置,然后插入进去,其他在它后面的元素全部后移,下面是普通插排的代码: 1 #include<iostream> 2 #include<fstream> 3 #include<stdio.h> 4 using namespace std; 5 int a[200000]; 6 int p[200000]; 7 8 int main(){ 9 ios::sync_wi

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the