Textbook Covers
Autumn Tournament (2021)
Submission name: bindings.cpp
Problem summary
Choose a rectangular cover for a textbook from two available covers. For every rectangle, width means its smaller side and height its larger side. A cover fits when its width exceeds the textbook width by at least 10 mm (5 mm on each side), and its height is at least the textbook height. If both fit, choose the one of smaller width.
Source clarification. The source does not specify a tie-break when both fitting covers have equal width but different heights. This implementation chooses the first; either has the required minimum width.
Input
Three lines describe the textbook and then covers 1 and 2. Each line gives cm1 mm1 cm2 mm2 for the two sides.
Output
Print 0 if neither cover fits. Otherwise print its index, and on a second line its width and height in centimetres with one decimal digit.
Constraints
The statement bounds each positive width and height by 10000. Dimensions are supplied in centimetres and millimetres.
Worked example
Original statement, example 1.
Input
15 3 10 0 7 1 15 3 18 0 13 5
Output
2 13.5 18.0
Solution analysis
- Convert every side to millimetres using 10 × centimetres + millimetres. Normalize its two sides into width = minimum and height = maximum.
- Check each cover against width ≥ textbook width + 10 and height ≥ textbook height. Among fitting covers retain the one of least width. Integer millimetres preserve exact comparisons.
- The textbook has width 100 mm and height 153 mm. Cover 1 is only 71 mm wide; cover 2 is 135 by 180 mm and fits, so print 2 and 13.5 18.0.
Why it works
Normalization implements the definition of width. The two inequalities are exactly the stated fitting requirements. Examining both covers rejects every invalid choice and selects a minimum-width valid one, or reports 0 when none exists.
Watch out. The allowance is 10 mm in total. Normalize each rectangle independently and print a trailing .0 for whole centimetres.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int width[3],height[3];
for(int i=0;i<3;++i){
int c1,m1,c2,m2;
cin>>c1>>m1>>c2>>m2;
int a=10*c1+m1;
int b=10*c2+m2;
width[i]=min(a,b);
height[i]=max(a,b);
}
int chosen=0;
for(int i=1;i<=2;++i){
if(width[i]>=width[0]+10&&height[i]>=height[0]){
if(chosen==0||width[i]<width[chosen]){
chosen=i;
}
}
}
cout<<chosen<<'\n';
if(chosen!=0){
// Integer quotient and remainder give exact tenths of centimetres.
cout<<width[chosen]/10<<'.'<<width[chosen]%10<<' '<<height[chosen]/10<<'.'
<<height[chosen]%10<<'\n';
}
}