PS

BOJ 2175 : 땅 자르기

lickelon 2024. 3. 12. 16:49
  • 문제 링크 : boj.kr/2175
  • 난이도 : G4
  • 태그 : 브루트포스, 기하학
 

2175번: 땅 자르기

첫째 줄에 사각형의 네 꼭짓점의 좌표가 순서대로(시계방향이나 반시계방향으로) 주어진다. 각 꼭짓점의 좌표는 절댓값이 10,000을 넘지 않는 정수이다. 입력으로 주어지는 사각형은 볼록 사각형

www.acmicpc.net


코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

ld area(vector<pll> dots) {
    ld sum = 0;
    ll s = dots.size();
    for(int i = 0; i < s; i++) {
        sum += dots[i].first * dots[(i+1)%s].second;
        sum -= dots[i].second * dots[(i+1)%s].first;
    }
    sum = abs(sum);
    return sum / 2;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout.precision(3);
    cout << fixed;

    pll dots[4];
    for(int i = 0; i < 4; i++) {
        cin >> dots[i].first >> dots[i].second;
    }

    vector<pll> arr(8);
    for(int i = 0; i < 4; i++) {
        arr[i*2] = {dots[i].first * 2, dots[i].second * 2};
        arr[i*2+1] = {dots[i].first + dots[(i+1)%4].first, 
                      dots[i].second + dots[(i+1)%4].second};
    }
    for(int i = 0; i < 8; i++) {
        arr.emplace_back(arr[i]);
    }
    ld total = area(vector<pll>(dots, dots+4));
    ld gap = total;
    for(int i = 0; i < 16; i++) {
        for(int j = i; j < 16; j++) {
            ld temp = area(vector<pll>(arr.begin()+i, arr.begin()+j)) / 4;
            ld t_gap = abs((total - temp) - temp);
            gap = min(gap, t_gap);
        }
    }
    cout << (total-gap)/2 << " " << (total+gap)/2;

    return 0;
}

풀이

여기에 풀이 작성 예정

'PS' 카테고리의 다른 글

BOJ 13884 : 삭삽 정렬  (0) 2024.03.14
BOJ 20158 : 사장님 달려가고 있습니다  (0) 2024.03.13
BOJ 12438 : 새로운 달력 (Large)  (0) 2024.03.11
BOJ 2187 : 점 고르기  (0) 2024.03.10
BOJ 1885 : 비부분수열  (0) 2024.03.09