CCC '07 S2 - Boxes
Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef queue<int> qi;
typedef queue<char> qc;
typedef stack<int> si;
typedef stack<char> sc;
int n;
struct box{
public:
int l;
int w;
int h;
int size;
};
bool comp(box lhs, box rhs) {
return lhs.size < rhs.size;
}
int check(box boxes[], box t){
int i;
for(i = 0; (i < n && (t.l > boxes[i].l || t.w > boxes[i].w || t.h > boxes[i].h)); i++)
;
if(i < n) return boxes[i].size;
return -1;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin >> n;
box boxes[n];
int l, w, h;
for(int i = 0; i < n; i++){
cin >> l >> w >> h;
if (l > w){
t = l;
l = w;
w = t;
}
if (w > h){
t = w;
w = h;
h = t;
}
if (l > w){
t = l;
l = w;
w = t;
}
boxes[i] = {l, w, h, l * w * h};
}
sort(boxes, boxes + sizeof(boxes)/sizeof(boxes[0]), comp);
int m;
cin >> m;
for(int i = 0; i < m; i++){
cin >> l >> w >> h;
if (l > w){
t = l;
l = w;
w = t;
}
if (w > h){
t = w;
w = h;
h = t;
}
if (l > w){
t = l;
l = w;
w = t;
}
box item = {l, w, h, l * w * h};
int checkv = check(boxes, item);
cout << ((checkv == -1) ? "Item does not fit." : to_string(checkv)) << "\n";
}
return 0;
}