cppAcademy
FROM LEARNING TO COMPETING

Real problems.
Your next challenge.

Explore the Group C Olympiad archive.
Choose a topic. Find a challenge. Build your solution.

213 tasks across the Municipal, Regional, and National rounds (2009–2026), plus 4 previously featured problems. 146 tasks have independently written English summaries, analyses, and C++17 solutions; the remaining entries link to Arena. Topic tags are from Arena or our reviewed explanations. Difficulty labels are learning estimates, not official ratings.
GROUP C2010 · MediumSorting Faces

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

  1. Normalize each face so its shorter side comes first, then sort the six pairs.
  2. Opposite faces must form three equal pairs. For dimensions a≤b≤c, the sorted distinct face slots are (a,b),(a,c),(b,c).
  3. 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.

Complexity: O(1) time and space (six faces).

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';
}

Source: Arena Olimpiici ↗