问题 B: Building Boundaries
时间限制: 1 S e c 1 Sec 1Sec 内存限制: 128 M B 128 MB 128MB
题目描述
Maarja wants to buy a rectangular piece of land and then construct three buildings on that land.
The boundaries of the buildings on the ground must have rectangular sizes a1 × b1 , a2 × b2 ,and a3 × b3 . They can touch each other but they may not overlap. They can also be rotated as long as their sides are horizontal and vertical.
What is the minimum area of land Maarja has to buy?
Figure B.1: Illustration of the two test scenarios in Sample Input 1 and their solutions. In the second scenario the 5 × 1 building has been rotated by 90 degrees.
输入
The input consists of multiple test scenarios. The first line of input contains a single integer t(1 ≤ t ≤ 1000), the number of scenarios. Then follow the t scenarios. Each scenario consists of a single line, containing six integers a1 , b1 , a2 , b2 , a3 and b3 (1 ≤ a1 , b1 , a2 , b2 , a3 , b3 ≤ 109 ).
输出
For each test scenario, output the minimum area of land such that Maarja can construct the three buildings.
样例输入 Copy
2
2 3 2 2 1 1
2 4 5 1 2 3
样例输出 Copy
12
21
题目大意:
分别给出三个矩形的长和宽,求一个最小的矩形能够把三个矩形都放进去,且放进去的三个矩形之间不发生重叠。
解题思路:
逐一枚举三个矩形组合的所有方案,算出每个组合方案的矩形面积,并取一个最小值。
组合情况,可大致分为以下两种
1、三个矩形各有一条边在同一直线上,如下图:
2、两个矩形各有一条边在同一直线上,如下图:
按照每种情况各自的组合方式,列出所有的方案即可,代码也比较有条理,而且还很清晰。
上代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[10][2];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i = 1;i <= 3;i++)
{
scanf("%lld%lld",&a[i][0],&a[i][1]);
if(a[i][0] > a[i][1]) swap(a[i][0],a[i][1]);
}
ll ans = 5e18;
//1.三个放成一排
for(int i = 0;i < 2;i++)//三个for循环分别枚举第一个,第二个,第三个分别使用的是哪条边
{
for(int j = 0;j < 2;j++)
{
for(int k = 0;k < 2;k++)
{
ans = min(ans,(a[1][i] + a[2][j] + a[3][k]) * max(a[1][!i],max(a[2][!j],a[3][!k])));
}
}
}
//2.两个放一起
/*1和2放一起*/
for(int i = 0;i < 2;i++)
{
for(int j = 0;j < 2;j++)
{
for(int k = 0;k < 2;k++)
{
ans=min(ans,max(a[1][i]+a[2][j],a[3][k])*max(a[1][!i]+a[3][!k],a[2][!j]+a[3][!k]));
}
}
}
/*1和3放一块儿*/
for(int i = 0;i < 2;i++)
{
for(int j = 0;j < 2;j++)
{
for(int k = 0;k < 2;k++)
{
ans=min(ans,max(a[1][i]+a[3][k],a[2][j])*max(a[1][!i]+a[2][!j],a[3][!k]+a[2][!j]));
}
}
}
/*2和3放一块儿*/
for(int i = 0;i < 2;i++)
{
for(int j = 0;j < 2;j++)
{
for(int k = 0;k < 2;k++)
{
ans=min(ans,max(a[2][j]+a[3][k],a[1][i])*max(a[2][!j]+a[1][!i],a[3][!k]+a[1][!i]));
}
}
}
cout<<ans<<endl;
}
return 0;
}