Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26-yuyu0830 #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

26-yuyu0830 #95

wants to merge 1 commit into from

Conversation

yuyu0830
Copy link
Collaborator

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

์„ ๋ถ„ ๊ทธ๋ฃน

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

4์‹œ๊ฐ„

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

โ“ ๋ฌธ์ œ

image

โ— ํ’€์ด

๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด ํฌ๊ฒŒ 2๊ฐ€์ง€ ๊ณผ์ œ๊ฐ€ ์žˆ๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์„ ๋ถ„ ๊ต์ฐจ ํŒ์ • ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋ถ„๋ฆฌ ์ง‘ํ•ฉ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

1๏ธโƒฃ ์„ ๋ถ„ ๊ต์ฐจ ํŒ์ •

์ €๋ฒˆ์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ ์—์„œ๋„ ์„ ๋ถ„ ๊ต์ฐจ ํŒ์ •์„ ๋‹ค๋ฃฌ ์ ์ด ์žˆ๋‹ค. ๊ทธ ๋•Œ ์‚ฌ์šฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์„ ๋ถ„์„ ์ง์„ ์œผ๋กœ ๋งŒ๋“  ๋’ค, ์ง์„ ์˜ ๊ต์ฐจ์ ์„ ์ฐพ์•„ ๊ต์ฐจ์ ์ด ๋‘ ์„ ๋ถ„ ์œ„์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

๋‹น์‹œ์— ๋„ˆ๋ฌด๋‚˜๋„ ๋งŽ์€ ์˜ˆ์™ธ ์ƒํ™ฉ์— ๊ณ ์ƒํ–ˆ๋˜ ๊ธฐ์–ต์ด ๋‚œ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์—” ์–Œ์ „ํžˆ CCW ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

CCW ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์™ธ์ ์„ ์‚ฌ์šฉํ•ด ์„ ๋ถ„์˜ ๊ต์ฐจ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•œ๋‹ค(์™€ ์™ธ์  ์ง„์งœ ์˜ค๋žœ๋งŒ) ์™ธ์ ์€ ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ๋‘ ๋ฒกํ„ฐ์— ๋ชจ๋‘ ์ˆ˜์งํ•˜๋Š” ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‰ํ–‰ํ•˜์ง€ ์•Š์€ ๋‘ ๋ฒกํ„ฐ์— ๋ชจ๋‘ ์ˆ˜์ง์ด๊ธฐ ์œ„ํ•ด z ์ถ• ์ƒ์˜ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. (์™ธ์ ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ธด ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™์œผ๋‹ˆ ์ฐธ๊ณ ํ•œ ๋งํฌ๋ฅผ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค)

image



๋‘ ๋ฒกํ„ฐ๋Š” 3๊ฐ€์ง€ ๋ฐฉํ–ฅ์„ฑ์„ ๋Œ ์ˆ˜ ์žˆ๋‹ค.

image



์ด ๋•Œ, ์™ธ์ ์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š” ๋ฒกํ„ฐ๋Š” ํ‰ํ–‰์„ ์ œ์™ธํ•œ ํšŒ์ „ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ z์ถ•์—์„œ ์–‘, ํ˜น์€ ์Œ์˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค.

ํšŒ์ „ ๋ฐฉํ–ฅ์— ๋”ฐ๋ฅธ z์ถ•์—์„œ์˜ ์–‘, ํ˜น์€ ์Œ์˜ ๊ฐ’์€ ์˜ค๋ฅธ์† ๋ฒ•์น™์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๋ฒกํ„ฐ์˜ ํšŒ์ „ ๋ฐฉํ–ฅ์„ ๋”ฐ๋ผ ์˜ค๋ฅธ์† ๊ทธ๋ฆฝ์„ ์ฅ์—ˆ์„ ๋•Œ ์—„์ง€์˜ ๋ฐฉํ–ฅ์ด z์ถ•์˜ ๋ฐฉํ–ฅ์ด ๋œ๋‹ค.

image



CCW ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ํšŒ์ „ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ z์ถ• ์—์„œ์˜ ๊ฐ’์ด ์Œ, ํ˜น์€ ์–‘ ์œผ๋กœ ๋‚˜์˜จ๋‹ค๋Š” ์ ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ต์ฐจํ•˜๋Š” ๋‘ ์„ ๋ถ„ A (x, y), B (w, z)๋ฅผ ๊ทธ๋ ค๋ณด์ž.

image



์„ ๋ถ„ A ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ณด๋ฉด A๊ฐ€ B๋ฅผ ๋ฐ˜์œผ๋กœ ๊ฐ€๋ฅด๊ณ  ์žˆ๋‹ค. ์ฆ‰ A๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ดค์„ ๋•Œ B๋ฅผ ์ด๋ฃจ๋Š” ์  w, z๋Š” ์„œ๋กœ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์— ์œ„์น˜ํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ์ด๋Ÿฌ๋ฉด ์•„๊นŒ ํšŒ์ „ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ z์ถ•์˜ ๊ฐ’์ด ์–‘์ˆ˜ ํ˜น์€ ์Œ์ˆ˜๋กœ ๋‚˜์˜ค๋Š” ์ ์„ ์ด์šฉํ•  ์ˆ˜ ์—†์„๊นŒ?

image



๊ต์ฐจํ•˜๋Š” ์„ ๋ถ„์€ ๋ฌด์กฐ๊ฑด ํ•œ ์„ ๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ๋ดค์„ ๋•Œ ๋‹ค๋ฅธ ์„ ๋ถ„์„ ์ด๋ฃจ๋Š” ์ ์ด ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์— ์œ„์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์ ์— ๋Œ€ํ•ด ์™ธ์ ์„ ํ•˜๋ฉด ๋ฌด์กฐ๊ฑด ์–‘์ˆ˜ ํ•˜๋‚˜, ์Œ์ˆ˜ ํ•˜๋‚˜๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค!

ํ•˜์ง€๋งŒ ํ•œ ์„ ์„ ๊ธฐ์ค€์œผ๋กœ๋งŒ ํ™•์ธํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ˆ์™ธ๊ฐ€ ์ƒ๊ธด๋‹ค.

image



๋‘ ์„ ๋ถ„์€ ๊ต์ฐจํ•˜์ง€ ์•Š์ง€๋งŒ ์„ ๋ถ„ B ๊ธฐ์ค€์œผ๋กœ ๋ดค์„ ๋•Œ x, y๋Š” ์„œ๋กœ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์— ์œ„์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์Œ์ˆ˜, ์–‘์ˆ˜ ํ•˜๋‚˜์”ฉ ๋‚˜์˜จ๋‹ค. ์ฆ‰ ๋‘ ์„ ๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌํ–ˆ์„ ๋•Œ ๊ฐ๊ฐ ์Œ์ˆ˜ ํ•œ๋ฒˆ, ์–‘์ˆ˜ ํ•œ๋ฒˆ ๋‚˜์˜ค๋ฉด ์ด ๋‘ ์„ ๋ถ„์€ ๊ต์ฐจํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค!



2๏ธโƒฃ ๋ถ„๋ฆฌ์ง‘ํ•ฉ

๋ฌธ์ œ์—์„œ ์ตœ๋Œ€ 3000๊ฐœ์˜ ์„ ๋ถ„์ด ์ฃผ์–ด์ง„๋‹ค. ์ฒ˜์Œ์—” ๋ชจ๋“  ์„ ๋ถ„๋ผ๋ฆฌ ๋น„๊ต๋Š” ๋„ˆ๋ฌด ๋งŽ์€ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ƒ๊ธธ ๊ฒƒ ๊ฐ™์•„ ์ค„์ผ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ CCW๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ž‘์•˜๊ณ , 3000 * (3000 - 1) / 2 ๋ฅผ ๊ตฌํ•ด๋ด์•ผ 4,500,000๋ฒˆ ์—ฐ์‚ฐ์ด๋ผ ์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์•˜๋‹ค.

๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ์„ ๋ถ„ ๋ผ๋ฆฌ ๋ชจ๋‘ ์—ฐ์‚ฐํ•ด์„œ CCW ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ–ˆ์„ ๋•Œ ๊ต์ฐจํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ–ˆ๋‹ค.

์†Œ์Šค์ฝ”๋“œ

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

using namespace std;

struct Point {
    int x, y;
    void set(int X, int Y) {
        x = X;
        y = Y;
    }

    bool operator== (const Point t) {
        return x == t.x && y == t.y;
    }
};

struct Line {
    Point a, b;

    void set(int x1, int y1, int x2, int y2) {
        bool e;

        // x๊ฐ€ ์ž‘์€ ์ˆœ, ๊ฐ™๋‹ค๋ฉด y๊ฐ€ ํฐ ์ˆœ
        // ์‚ฌ๋ถ„๋ฉด์—์„œ ์™ผ์ชฝ ์œ„ ์ ์„ ์šฐ์„ 
        if (x1 != x2) e = x1 < x2;
        else e = y1 > y2;
        
        a.x = e ? x1 : x2;
        a.y = e ? y1 : y2;
        b.x = e ? x2 : x1;
        b.y = e ? y2 : y1;
    }
};


// CCW
Line arr[3001];
bool visited[3001] = {0, };

int CCW(Point a, Point b, Point c) {
    int t1 = a.x * b.y + b.x * c.y + c.x * a.y;
    int t2 = a.y * b.x + b.y * c.x + c.y * a.x;

    int s = t1 - t2;

    if (s > 0) return 1;
    else if (!s) return 0;
    else return -1;
}

bool isIntersect(Line l1, Line l2) {
    Point p1 = l1.a;
    Point p2 = l1.b;
    Point p3 = l2.a;
    Point p4 = l2.b;

    if (p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4) return true;

    int t1 = CCW(p1, p2, p3) * CCW(p1, p2, p4); // l1 ๊ธฐ์ค€
    int t2 = CCW(p3, p4, p1) * CCW(p3, p4, p2); // l2 ๊ธฐ์ค€

    // ๋‘ ์ง์„ ์ด ์ผ์ง์„  ์ƒ์— ์กด์žฌํ•˜๋ฉด
    // ๊ธฐ์šธ๊ธฐ๊ฐ€ 0 ํ˜น์€ ๋ฌดํ•œ์ธ ๊ฒฝ์šฐ || ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ
    if (!t1 && !t2) {
        bool x = (p1.x == p3.x && p2.x == p4.x) || (p1.x <= p4.x != p2.x <= p3.x);
        bool y = (p1.y == p3.y && p2.y == p4.y) || (p1.y >= p4.y != p2.y >= p3.y);

        return x && y;
    }

    return t1 <= 0 && t2 <= 0;
}

// union find
int parent[3001] = {0, };
int count[3001] = {0, };

int find(int v) {
    if (parent[v] == v) return v;
    return parent[v] = find(parent[v]);
}

void merge(int x, int y) { 
    parent[find(min(x, y))] = find(max(x, y));
}

int main() {
    int n; cin >> n;

    for (int i = 0; i < n; i++) {
        int a, b, c, d; cin >> a >> b >> c >> d;
        arr[i].set(a, b, c, d);

        parent[i] = i;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            // i๋ฒˆ์งธ Line๋Š” 0 ~ i - 1๋ฒˆ์งธ Line๊นŒ์ง€ ๊ฒน์น˜๋Š”์ง€ ํ™•์ธ
            // ๊ฒน์น˜๋ฉด Union ํ•ด๋ฒ„๋ฆฌ๊ธฐ
            if (isIntersect(arr[i], arr[j])) 
                merge(i, j);
        }
    }
    
    // ๊ฒน์นจ ์—ฌ๋ถ€๊ฐ€ ๋’ค์— ๊ฒฐ์ •๋  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์ค‘์— ๋ชฐ์•„์„œ ๊ฐœ์ˆ˜ ์—ฐ์‚ฐ
    for (int i = 0; i < n; i++) 
        ::count[find(i)]++;

    int cnt = 0, maxCnt = 0;

    // ๊ฐœ์ˆ˜ ๊ฐฑ์‹ 
    for (int i = 0; i < n; i++) {
        if (::count[i]) {
            cnt++;
            maxCnt = max(maxCnt, ::count[i]);
        } 
    }

    printf("%d\n%d\n", cnt, maxCnt);
}

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

์ด๋ฒˆ PR ๋„ˆ๋ฌด ๊ธธ์–ด์š” ใ… ใ… 

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

CCW๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ์ €๋กœ์จ๋Š” ๊ทธ์ € ์ˆ˜ํ•™ + ๊ตฌํ˜„ ๋ฌธ์ œ๋กœ ๋ฐ–์— ์•ˆ๋ณด์ด๋”๊ตฐ์š”...

๋‘ ์„ ๋ถ„์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ฐ ์ ์„ ๋น„๊ตํ•ด์„œ ๋‚ด์ , ์™ธ์ ์ธ ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ฅธ ์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด๋ฌธ๋“ค๋กœ ํƒ์ƒ‰ํ–ˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
x1 x2, x3, y4๊ฐ€ ์žˆ์„ ๋•Œ x1์ด x3๋ณด๋‹ค ์ž‘๋‹ค๋ฉด x2๋Š” x3๋ณด๋‹ค๋Š” ์ปค์•ผํ•˜๋Š” ์‹์œผ๋กœ..

CCW ๊ฐœ๋…์€ ์•„์ง ์ œ๋Œ€๋กœ ์žกํžˆ์ง€ ์•Š์•˜์ง€๋งŒ ์ค‘์‹ฌ์ถ• ๊ธฐ์ค€์œผ๋กœ ์™ผ์† ์ขŒํ‘œ๊ณ„, ์˜ค๋ฅธ์† ์ขŒํ‘œ๊ณ„๊ฐ€ ๊ธฐ์–ต์ด ๋– ์˜ค๋ฅด๊ธด ํ•˜์ง€๋งŒ ์ด๊ฒŒ ๋งž๋Š”์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๊ตฐ์š”

์™ธ์—๋„ ๊ฒน์น˜๋Š” ์„ ๋ถ„์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ UnionFind๋ฅผ ํ†ตํ•ด ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹ ์ž˜ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์— ๊ธฐํšŒ๊ฐ€ ๋œ๋‹ค๋ฉด ํ’€์–ด๋ด์•ผ ๊ฒ ๋„ค์š”

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.

3 participants