-
1555-B, *1300코드포스 2021. 7. 31. 15:16
Problem - 1555B - Codeforces
codeforces.com
풀이
- 너비가 W, 높이가 H인 배치도에 2개의 x-y축에 나란한(axis-aligned) 두 개의 책상을 배치한다
- 첫번째 책상이 먼저 배치되어 있다
- 두번째 책상의 너비w와 높이h가 주어질 때, 첫번째 책상을 이동하여 두 번째 책상을 배치할 수 있나?
있으면 첫번째 책상을 최소 얼만큼 움직여야함?
O(t)
책상을 배치하는 경우는 다음과 같이 나눠진다
첫번째 책상을 기준으로 두 번째 책상은
i) 상, 하에만 배치 가능
ii) 좌, 우에만 배치 가능
iii) 상, 하, 좌, 우 배치 가능
iv) 안됨1234567891011121314151617181920212223242526272829303132333435363738394041424344#include <bits/stdc++.h>#define endl "\n"using namespace std;int W, H;int x_1, y_1, x_2, y_2;int w, h;int sufficient_a(int a, int gap){if(a <= gap) return 0;else return a-gap;}int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int t;cin >> t;while(t--){cin >> W >> H;cin >> x_1 >> y_1 >> x_2 >> y_2;cin >> w >> h;if(w > W-(x_2-x_1) && h > H-(y_2-y_1)) cout << -1 << endl;else{if(w <= W-(x_2-x_1) && h > H-(y_2-y_1)) {cout << min(sufficient_a(w, x_1), sufficient_a(w, W-x_2)) << endl;}else if(w > W-(x_2-x_1) && h <= H-(y_2-y_1)) {cout << min(sufficient_a(h, y_1), sufficient_a(h, H-y_2)) << endl;}else{int dx = min(sufficient_a(w, x_1), sufficient_a(w, W-x_2));int dy = min(sufficient_a(h, y_1), sufficient_a(h, H-y_2));cout << min(dx, dy) << endl;}}}return 0;}cs'코드포스' 카테고리의 다른 글
1534-C, *1300 (0) 2021.08.01 1538-C, *1300 (0) 2021.08.01 1555-C, *1300 (0) 2021.07.31 1547-D, *1300 (0) 2021.07.30 1553-B, *1300 (0) 2021.07.28