[搜索]USACO-1.4-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


至于如何确定搜索策略,还是看了解题报告才知道了,题目有点阴,也怪自己没仔细看,原来总共的摆放方式已经有图给出了,那么直接把四个矩形往上面填就可以了,用DFS实现,类似于求全排列,不过最后一种摆放方式要注意处理,计算盒子面积时要分情况讨论一下就好了。

代码:

/*
ID:yfr_1992
PROG:packrec
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
struct Rectangle{
    int w,h;
}recs[8],temp[4];
vector< pair<int,int> > res;
bool vis[8],hash[500][500];
int minarea = 0x3fffffff;
void read(){
    for(int i=0;i<8;i+=2){
        scanf("%d %d",&recs[i].w,&recs[i].h);
        recs[i+1].w = recs[i].h , recs[i+1].h = recs[i].w;
    }
}
void dfs(int cur){
    if(cur==4){
        /*for(int i=0;i<cur;i++){
            printf("%d %d\n",temp[i].w,temp[i].h);
        }
        system("pause");*/
        int w,h;
case1:
        w = temp[0].w+temp[1].w+temp[2].w+temp[3].w;
        h = max(temp[0].h,temp[1].h);
        h = max(h,temp[2].h);
        h = max(h,temp[3].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case2:
        w = max(temp[0].w,temp[1].w+temp[2].w+temp[3].w);
        h = max(temp[1].h,temp[2].h);
        h = max(temp[3].h,h)+temp[0].h;
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case3:
        w = max(temp[0].w,temp[1].w+temp[2].w)+temp[3].w;
        h = max(max(temp[1].h,temp[2].h)+temp[0].h,temp[3].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case4:
        w = temp[0].w+temp[3].w+max(temp[1].w,temp[2].w);
        h = max(max(temp[0].h,temp[3].h),temp[1].h+temp[2].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case5:
        w = max(temp[0].w,temp[1].w)+temp[2].w+temp[3].w;
        h = max(max(temp[2].h,temp[3].h),temp[0].h+temp[1].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case6:
        //这种情况注意处理,根据接触面来处理
        if(temp[0].h>temp[3].h){
            if(temp[0].h<temp[3].h+temp[2].h){
                w = max(max(temp[1].w+temp[2].w,temp[0].w+temp[3].w),temp[0].w+temp[2].w);
            }else{
                w = max(max(temp[0].w+temp[3].w,temp[0].w+temp[2].w),temp[1].w);
            }
        }else{
            if(temp[0].h+temp[1].h>temp[3].h){
                w = max(max(temp[1].w+temp[2].w,temp[3].w+temp[0].w),temp[3].w+temp[1].w);
            }else{
                w = max(max(temp[3].w+temp[0].w,temp[3].w+temp[1].w),temp[2].w);
            }
        }
        h = max(temp[0].h+temp[1].h,temp[2].h+temp[3].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
        return;
    }
    for(int i=0;i<8;i++){
        int idx = i/2;
        if(!vis[idx<<1|1]&&!vis[idx<<1]){
            vis[i] = 1;
            temp[cur] = recs[i];
            dfs(cur+1);
            vis[i] = 0;
        }
    }
}
bool p_cmp(const pair<int,int> &a,const pair<int,int> &b){
    return a.first<b.first;
}
int main(){
    freopen("packrec.in","r",stdin);
    freopen("packrec.out","w",stdout);
    read();
    dfs(0);
    sort(res.begin(),res.end(),p_cmp);
    //while(1);
    printf("%d\n",res[0].first*res[0].second);
    for(int i=0;i<(int)res.size();i++){
        printf("%d %d\n",res[i].first,res[i].second);
    }
    return 0;
}



### USACO 1327 Problem Explanation USACO 1327涉及的是一个贪心算法中的区间覆盖问题。具体来说,这个问题描述了一组奶牛可以工作的班次范围,并要求找出最少数量的奶牛来完全覆盖所有的班次。 对于此类问题的一个有效方法是采用贪心策略[^1]。首先按照区间的结束时间从小到大排序这些工作时间段;如果结束时间相同,则按开始时间从早到晚排列。接着遍历这个有序列表,在每一步都尽可能选择最早能完成当前未被覆盖部分的工作时段。通过这种方式逐步构建最终解集直到所有的时间段都被覆盖为止。 为了提高效率并防止超时错误,建议使用`scanf()`函数代替标准输入流操作符`cin`来进行数据读取处理[^2]。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start; int end; }; bool compareIntervals(const Interval& i1, const Interval& i2) { return (i1.end < i2.end || (i1.end == i2.end && i1.start < i2.start)); } int main() { vector<Interval> intervals = {{1, 7}, {3, 6}, {6, 10}}; sort(intervals.begin(), intervals.end(), compareIntervals); int currentEnd = 0; int count = 0; for (const auto& interval : intervals) { if (interval.start > currentEnd) break; while (!intervals.empty() && intervals.front().start <= currentEnd) { if (intervals.front().end >= interval.end) { interval = intervals.front(); } intervals.erase(intervals.begin()); } currentEnd = interval.end; ++count; if (currentEnd >= 10) break; // Assuming total shift length is known. } cout << "Minimum number of cows needed: " << count << endl; } ``` 此代码片段展示了如何实现上述提到的方法解决该类问题。需要注意的是实际比赛中可能还需要考虑更多边界条件以及优化细节以满足严格的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值