สมมติว่าเรามีกล่อง n คู่ที่ต้องจัดส่งในภาชนะรูปสี่เหลี่ยมจัตุรัส ความกว้างของคู่ของกล่องถูกกำหนดเป็นคู่ (a, b) และกำหนดไว้ในอาร์เรย์'มิติ' หากเรารักษาความกว้างของกล่องให้ขนานกัน เราต้องค้นหาว่ากล่องจะใช้พื้นที่ภายในคอนเทนเนอร์เท่าใด เราไม่สามารถวางกล่องทับซ้อนกันได้ เรากำหนดพื้นที่ขั้นต่ำที่กล่องสองกล่องในคอนเทนเนอร์ต้องการสำหรับ npair ทั้งหมด
ดังนั้น หากอินพุตเป็น n =4 มิติข้อมูล ={{2, 4}, {3, 6}, {2, 5}, {4, 6}} ผลลัพธ์จะเป็น −
64 25 36 16
ขั้นตอน
เพื่อแก้ปัญหานี้ เราจะทำตามขั้นตอนเหล่านี้ -
res := 0 while n is non-zero, do: a := first value of dimensions[n] b := second value of dimensions[n] res := maximum of (2 * minimum of (a, b) and maximum of a and b) print(res * res) n := n - 1
ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, vector<pair<int, int>> dimensions) {
int res = 0;
while(n--){
int a = dimensions[n].first;
int b = dimensions[n].second;
int res = max(2 * min(a, b), max(a, b));
cout<< res * res << endl;
}
}
int main() {
int n = 4;
vector<pair<int, int>> dimensions = {{2, 4}, {3, 6}, {2, 5}, {4, 6}};
solve(n, dimensions);
return 0;
} อินพุต
4, {{2, 4}, {3, 6}, {2, 5}, {4, 6}} ผลลัพธ์
64 25 36 16