Skip to content

Conversation

@froglike6
Copy link
Collaborator

@froglike6 froglike6 commented Jul 13, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Cows

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

์˜ˆ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋ผ ํ‘ผ ์‹œ๊ฐ„์ด ๊ธฐ์–ต์€ ๋‚˜์ง€ ์•Š์ง€๋งŒ, 1์‹œ๊ฐ„ ์ •๋„ ๊ฑธ๋ฆฐ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ˆ˜๋„ ์ฝ”๋“œ

์ž…๋ ฅ P

์  ๋ชฉ๋ก points๋ฅผ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ ๋‹ค
P๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ:
    (x, y)๋ฅผ ์ž…๋ ฅ๋ฐ›์•„
    points์— (x, y)๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

cross(o, a, b) ํ•จ์ˆ˜:
    (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

convex_hull(points) ํ•จ์ˆ˜:
    points๋ฅผ ์ •๋ ฌํ•˜๊ณ  ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค
    ๋งŒ์•ฝ points์˜ ํฌ๊ธฐ๊ฐ€ 1 ์ดํ•˜๋ผ๋ฉด:
        points๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค

    lower๋ฅผ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ ๋‹ค
    points๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๋ณด๋ฉด์„œ:
        lower์— ์ ์ด ๋‘ ๊ฐœ ์ด์ƒ ์žˆ๊ณ 
        cross(lower[-2], lower[-1], ํ˜„์žฌ์ ) โ‰ค 0 ์ด๋ผ๋ฉด:
            lower์˜ ๋งˆ์ง€๋ง‰ ์ ์„ ์ œ๊ฑฐํ•œ๋‹ค
        current ์ ์„ lower์— ์ถ”๊ฐ€ํ•œ๋‹ค

    upper๋ฅผ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ ๋‹ค
    points๋ฅผ ๋์—์„œ๋ถ€ํ„ฐ ์ฒ˜์Œ๊นŒ์ง€ ์—ญ์ˆœ์œผ๋กœ ๋ณด๋ฉด์„œ:
        upper์— ์ ์ด ๋‘ ๊ฐœ ์ด์ƒ ์žˆ๊ณ 
        cross(upper[-2], upper[-1], ํ˜„์žฌ์ ) โ‰ค 0 ์ด๋ผ๋ฉด:
            upper์˜ ๋งˆ์ง€๋ง‰ ์ ์„ ์ œ๊ฑฐํ•œ๋‹ค
        current ์ ์„ upper์— ์ถ”๊ฐ€ํ•œ๋‹ค

    lower์˜ ๋งˆ์ง€๋ง‰ ์ ๊ณผ upper์˜ ๋งˆ์ง€๋ง‰ ์ (์ค‘๋ณต)์„ ์ œ์™ธํ•˜๊ณ :
        lower์™€ upper๋ฅผ ์ด์–ด ๋ถ™์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

shoelace_area(polygon) ํ•จ์ˆ˜:
    n โ† polygon์˜ ์  ๊ฐœ์ˆ˜
    area โ† 0
    i๋ฅผ 0๋ถ€ํ„ฐ n-1๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ:
        (x1, y1) โ† polygon[i]
        (x2, y2) โ† polygon[(i + 1) mod n]
        area์— (x1 * y2) - (x2 * y1)๋ฅผ ๋”ํ•œ๋‹ค
    ์ ˆ๋Œ“๊ฐ’(area)์„ 2๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค

๋ฉ”์ธ ๋กœ์ง:
    hull โ† convex_hull(points)
    pasture_area โ† shoelace_area(hull)
    answer โ† ์ •์ˆ˜ ๋ถ€๋ถ„๋งŒ ์ทจํ•œ (pasture_area // 50)
    answer๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค

ChatGPT Image 2025๋…„ 7์›” 14์ผ ์˜ค์ „ 01_40_13 ๋ชฉ์ดˆ์ง€์— ๋‚˜๋ฌด์™€ ์†Œ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šธํƒ€๋ฆฌ๋ฅผ ์‹ฌ์–ด์•ผ ํ•˜์ง€๋งŒ ๊ธฐ๋‘ฅ์„ ๋ฐ•๊ธฐ ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์—, ์‹ฌ๊ฒจ์ง„ ๋‚˜๋ฌด๋“ค๋กœ ์šธํƒ€๋ฆฌ ๊ธฐ๋‘ฅ์„ ๋Œ€์‹ ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ๋„“์ด์˜ ๋ชฉ์ดˆ์ง€๋ฅผ ์ผ๋‹จ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์†Œ๋“ค์€ ํ•œ ๋งˆ๋ฆฌ๋‹น 50์ œ๊ณฑ๋ฏธํ„ฐ์˜ ๋ชฉ์ดˆ์ง€๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ, ํ‚ค์šธ ์ˆ˜ ์žˆ๋Š” ์†Œ์˜ ์ตœ๋Œ€ ๋งˆ๋ฆฌ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๊ฒŒ ์ด ๋ฌธ์ œ์˜ ์ •๋‹ต์ž…๋‹ˆ๋‹ค.

๋‚˜๋ฌด๊ฐ€ ์ ์ฒ˜๋Ÿผ ์‹ฌ๊ฒจ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๋‹น์—ฐํžˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ผ ๋•Œ ์ตœ๋Œ€ ๋„“์ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

upper_hull_step_16

์ด ๋ฐ”๊นฅ ์ ๋“ค์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ•ด์•ผ ํ• ๊นŒ์š”? ๋ฐ”๋กœ Convex Hull์ด๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ค‘์—์„œ๋„ ์ €๋Š” Monotone chain์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

Monotone Chain ๋ฐฉ๋ฒ•์€ ์  3๊ฐœ๋ฅผ ์„ ํƒํ•˜์—ฌ ์™ธ์ ์„ ํ•œ ํ›„, ๊ทธ ๋ถ€ํ˜ธ๋กœ ๊ฐ€์žฅ ๋ฐ”๊นฅ์— ์žˆ๋Š” ์ ์„ ์„ ํƒํ•˜์—ฌ ๋ณผ๋ก๊ป์งˆ์„ $\mathcal{O}(n log n)$๋งŒ์— ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ezgif-528f06c1b9466e

์ด ์›€์ง์ด๋Š” ์‚ฌ์ง„์„ ๋ณด๋ฉด, ํ˜„์žฌ ์„ ํƒ๋œ ์ ์—์„œ ๋‹ค์Œ ์ (X๊ฐ’ ๊ธฐ์ค€)์„ ์„ ํƒํ•˜๊ณ  ์™ธ์ ์„ ํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒ๋ณ„ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™ธ์ ํ•œ ๊ฐ’์ด ์Œ์ˆ˜๋ผ๋ฉด ์˜ค๋ชฉ๋‹ค๊ฐํ˜•์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‹ค๋ฅธ ์ ์„ ์„ ํƒํ•˜์—ฌ ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ X๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์™ธ์ ์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์ด๋ฏธ #65 ์— ์ž˜ ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Žใ…Ž.

์ด ๋ฐฉ์‹์œผ๋กœ ์•„๋ž˜์ชฝ ๋ถ€๋ถ„์„ ๊ตฌํ•˜๊ณ , ์—ญ์ˆœํƒ์ƒ‰์œผ๋กœ ์œ„์ชฝ ๋ถ€๋ถ„์„ ๊ตฌํ•˜์—ฌ ๋‘˜์„ ์ด์–ด์ฃผ๋ฉด ๋ณผ๋ก๊ป์งˆ์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ทธ ๋„“์ด๋ฅผ ์‹ ๋ฐœ๋ˆ์œผ๋กœ ๊ตฌํ•˜๊ณ , 50์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ์„ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ž…๋‹ˆ๋‹ค!!

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@dohyeondol1
Copy link
Contributor

๋ช‡์ฃผ์ „์— ๋ชปํ’€์—ˆ๋˜ ๋ฌธ์ œ์ธ๋”” ๋‹ค์‹œ ๋„์ „ํ•ด๋ด์•ผ๊ฒ ๋„ค์š” ใ…Žใ…Ž

@froglike6 froglike6 self-assigned this Jul 21, 2025
@froglike6 froglike6 marked this pull request as ready for review July 21, 2025 05:13
@froglike6 froglike6 changed the base branch from 20-froglike6 to main July 22, 2025 05:03
Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ๋ณผ๋ก๊ป์งˆ์„ ๊ทธ๋ ˆ์ด์—„ ์Šค์บ”(Graham Scan) ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฐฐ์›Œ์„œ ์ด ํ’€์ด๋Š” ์ฒ˜์Œ ์ฝ์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
๋ฐฉ์‹์ด ๋˜๊ฒŒ ๋…ํŠนํ•˜๋„ค์š”..!!
๋‹ค์Œ์— ์‰ฌ์šด ๋ณผ๋ก๊ป์งˆ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณผ ๋•Œ ์ ์šฉํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ ๋ณผ๋ก๊ป์งˆ ๋ฌธ์ œ๋ฅผ ์ด์ „์— ํ’€์—ˆ๋˜ ๊ธฐ์–ต์ด ๋‚˜์„œ.. ํ’€์—ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์‹œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;

struct Point {
    long long x, y;
    int idx;
};

Point base;

long long crossProduct(const Point& a, const Point& b, const Point& c) {
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

long long distanceSquared(const Point& a, const Point& b) {
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

bool angleSort(const Point& a, const Point& b) {
    long long cross = crossProduct(base, a, b);
    if (cross != 0) return cross > 0;
    return distanceSquared(base, a) < distanceSquared(base, b);
}

double calculateArea(const vector<Point>& hull) {
    int n = hull.size();
    if (n < 3) return 0.0;
    
    long long area = 0;
    for (int i = 0; i < n; i++) {
        int j = (i + 1) % n;
        area += hull[i].x * hull[j].y;
        area -= hull[j].x * hull[i].y;
    }
    
    return abs(area) / 2.0;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N;
    cin >> N;
    
    vector<Point> points(N);
    for(int i = 0; i < N; ++i) {
        cin >> points[i].x >> points[i].y;
        points[i].idx = i;
    }

    int baseIdx = 0;
    for(int i = 1; i < N; ++i) {
        if(points[i].y < points[baseIdx].y || (points[i].y == points[baseIdx].y && points[i].x < points[baseIdx].x))
            baseIdx = i;
    }
    
    swap(points[0], points[baseIdx]);
    base = points[0];

    sort(points.begin() + 1, points.end(), angleSort);
    
    vector<Point> hull;
    for(int i = 0; i < N; ++i) {
        while(hull.size() >= 2) {
            if(crossProduct(hull[hull.size()-2], hull[hull.size()-1], points[i]) <= 0)
                hull.pop_back();
            else
                break;
        }
        hull.push_back(points[i]);
    }

    double area = calculateArea(hull);
    double result = area / 50.0;

    cout << (long long)result << '\n';
    return 0;
}

@dohyeondol1 dohyeondol1 removed the request for review from hadongun August 1, 2025 13:14
@Fnhid
Copy link
Collaborator

Fnhid commented Aug 4, 2025

์ €๋Š” ์ˆ˜๋„์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์งœ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

convex_hull()์—์„œ ์ ๋“ค์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด std::unique()๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, pair<int, int>์—์„œ x, y๋ฅผ ๋น„๊ตํ•  ๋•Œ ๋žŒ๋‹คํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์š”์ฆ˜ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๋‹ˆ ๋ฐ˜๊ฐ‘๋„ค์š”.

#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;
using Point = pair<int, int>;

#define X first
#define Y second


int cross(const Point& o, const Point& a, const Point& b) {
    return (a.X - o.X) * (b.Y - o.Y) - (a.Y - o.Y) * (b.X - o.X);
}

bool compare(const Point& a, const Point& b) {
    if (a.X == b.X) return a.Y < b.Y;
    return a.X < b.X;
}


vector<Point> convex_hull(vector<Point> p) {
    sort(p.begin(), p.end(), compare);
    auto it = unique(p.begin(), p.end(), [](const Point& a, const Point& b) {
        return a.X == b.X && a.Y == b.Y;
    });
    p.erase(it, p.end());

    if (p.size() <= 1) return p;

    vector<Point> lP;
    for (const auto& p : p) {
        while (lP.size() >= 2 && cross(lP[lP.size() - 2], lP.back(), p) <= 0) {
            lP.pop_back();
        }
        lP.push_back(p);
    }

    vector<Point> uP;
    for (auto it = p.rbegin(); it != p.rend(); ++it) {
        const auto& p = *it;
        while (uP.size() >= 2 && cross(uP[uP.size() - 2], uP.back(), p) <= 0) {
            uP.pop_back();
        }
        uP.push_back(p);
    }

    lP.pop_back();
    uP.pop_back();

    lP.insert(lP.end(), uP.begin(), uP.end());
    return lP;
}

double shoelace_area(const vector<Point>& polygon) {
    int n = polygon.size();
    double area = 0.0;
    for (int i = 0; i < n; ++i) {
        int x1 = polygon[i].X;
        int y1 = polygon[i].Y;
        int x2 = polygon[(i + 1) % n].X;
        int y2 = polygon[(i + 1) % n].Y;
        area += (x1 * y2) - (x2 * y1);
    }
    return abs(area) / 2.0;
}

int main() {
    int n;
    cin >> n;
    vector<Point> p;
    for (int i = 0; i < n; ++i) {
        int x, y;
        cin >> x >> y;
        p.push_back({x, y});
    }

    vector<Point> hull = convex_hull(p);
    double pasture_area = shoelace_area(hull);
    int answer = static_cast<int>(pasture_area) / 50;  
    cout << answer << endl;

    return 0;
}

@Fnhid Fnhid merged commit 1deba32 into main Aug 4, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants