ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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) 안됨

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    #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;

    '코드포스' 카테고리의 다른 글

    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

    댓글

Designed by Tistory.