Six Faces of a Box
National Olympiad — Regional round
Submission name: box.cpp
Problem summary
Six rectangular panels must be used as the six faces of a rectangular box. Panels may be rotated. Determine its volume if assembly is possible.
Input
Six pairs w h.
Output
The volume, or 0 if impossible.
Constraints
1 ≤ w,h ≤ 10000.
Worked example
An independently prepared example for this explanation.
Input
2 3 4 2 3 4 3 2 2 4 4 3
Output
24
Solution analysis
- Normalize each face so its shorter side comes first, then sort the six pairs.
- Opposite faces must form three equal pairs. For dimensions a≤b≤c, the sorted distinct face slots are (a,b),(a,c),(b,c).
- Check the shared dimensions and multiply a·b·c using 64 bits.
Why it works
Every rectangular box has exactly these paired faces. Conversely, matching these pairs constructs the required box.
Watch out. Cubes and repeated dimensions are valid; volume can exceed 32 bits.
Commented C++17 solution
An independently written complete program with standard headers.
// Key idea: Every rectangular box has exactly these paired faces. Conversely, matching these pairs
// constructs the required box.
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main(){
vector<pair<long long,long long>>f(6);
for(auto&[a,b]:f){
cin>>a>>b;
if(a>b){
swap(a,b);
}
}
sort(f.begin(),f.end());
bool ok=f[0]==f[1]&&f[2]==f[3]&&f[4]==f[5]&&f[0].first==f[2].first&&
f[0].second==f[4].first&&f[2].second==f[4].second;
cout<<(ok?f[0].first*f[0].second*f[2].second:0)<<'\n';
}