λ¬Έμ
λ λ€κ°νμ΄ μ£Όμ΄μ‘μ λ, λ λ€κ°νμ λ―Όμ½νμ€ν€ ν©μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€. λ§μ½ λ―Όμ½νμ€ν€ ν©μ΄ μ¬λ¬ κ°μ λ€κ°νμΌλ‘ μ΄λ£¨μ΄μ§λ€λ©΄ λ€μμ μ°μ μμμ λ°λΌ νλμ λ€κ°νλ§μ ꡬνλλ‘ νλ€. λ²νΈκ° μμ κ²μ΄ μ°μ μμκ° λμ κ²μ΄λ€.
μ λ ₯ 1
3 3
0 0
1 0
1 1
0 1
0 0
1 0
μΆλ ₯ 1
5
0 0
2 0
2 1
1 2
0 1
μ½λ
#include<iostream>
#include<algorithm>
#include<vector>
#define x first
#define y second
using namespace std;
//2244λ² λ―Όμ½νμ€ν€ ν©
//Convex Hull(CCW(Counter Clock Wise)) μ¬μ©
typedef pair<long long, long long> Point2f;
Point2f a[1010];
Point2f b[1010];
//counter clock wise : μ’νμ νλμ§ νλ¨
//λ²‘ν° ABμ λ²‘ν° ACμ CW/CCW
// 0λ³΄λ€ ν¬λ©΄ λ°μκ³λ°©ν₯μΌλ‘ λλ κ², 0λ³΄λ€ μμΌλ©΄ μκ³λ°©ν₯μΌλ‘ λλ κ²
//intμ 1LLμ κ³±ν΄μ£Όλ©΄ long longμΌλ‘ νλ³νμ μ½κ² ν μ μμ
int ccw(Point2f a, Point2f b, Point2f c) {
long long res = a.x * b.y + b.x * c.y + c.x * a.y;
res -= b.x * a.y + c.x * b.y + a.x * c.y;
if (res > 0) return 1; //μκ³ λ°λλ°©ν₯
if (res) return -1; //μκ³ λ°©ν₯
return 0; //νλ©΄μ
}
long long dist(Point2f a, Point2f b) {
long long dx = 1LL*a.x - b.x;
long long dy = 1LL * a.y - b.y;
return dx * dx + dy * dy;
}
int main() {
vector<Point2f> arr;
int aNum, bNum;
int xTmp, yTmp;
int pIdx = 0;
cin >> aNum >> bNum;
pIdx = aNum * bNum;
for (int i = 0; i < aNum; i++) {
cin >> xTmp >> yTmp;
a[i].x = xTmp; a[i].y = yTmp;
}
for (int i = 0; i < bNum; i++) {
cin >> xTmp >> yTmp;
b[i].x = xTmp; b[i].y = yTmp;
}
//λͺ¨λ κ°λ₯μ±μ΄ μλ μ λ€ vector μ μ
for (int i = 0; i < aNum; i++) {
for (int j = 0; j < bNum; j++) {
arr.push_back(Point2f{ a[i].x + b[j].x,a[i].y + b[j].y });
}
}
//arrμ μ΅μ κ°μ μ°Ύμμ 0λ² μ리μ λ°κΏ(0λ²μ μ μΌ μμ κ°μΌλ‘ μ€μ )
swap(arr[0], *min_element(arr.begin(), arr.end()));
//0λ²μ μ μΈν μ λ€μ λ°μκ³ λ°©ν₯μΌλ‘ μ λ ¬
//[&]{}λ λλ€μμΌλ‘μ¨ ν΄λΉ ν¨μμλ°λΌμ sortλ₯Ό ν μ§ λ§μ§ κ²°μ ν¨
sort(arr.begin() + 1, arr.end(), [&](Point2f& a, Point2f& b) {
int cw = ccw(arr[0], a, b);
//cwκ° 0 μ΄μμ΄λ©΄ λ°μκ³ λ°©ν₯μ΄κ³ trueλ₯Ό λ°ν 0μ΄λ©΄ false λ°ν, cwκ° -1μΈ κ²½μ°(μκ³ λ°©ν₯) ifλ¬Έμ λ€μ΄μ€μ§ μμ
if (cw) return cw > 0;
return dist(arr[0], a) < dist(arr[0], b); //cwκ° -1μΈ κ²½μ° κΈ°μ€μ arr[0]μμμ 거리μμΌλ‘ μ λ ¬
});
vector<Point2f> hull;
for (int i = 0; i < arr.size();i++) {
while (hull.size() >= 2 && ccw(hull[hull.size() - 2], hull.back(), arr[i]) <= 0) {
hull.pop_back();
}
hull.push_back(arr[i]);
}
cout << hull.size() << "\n";
for (int i = 0; i < hull.size(); i++) {
cout << hull[i].x << " " << hull[i].y << "\n";
}
return 0;
}
*κ°λ¨ν ν΄μ€*
convex hullλ‘ κ΅¬ννλ©΄ λλ λ¬Έμ μ΄λ€.
convex hullμ μ£Όμ΄μ§ μ μμ μΈκ³½μ μ μ΄λ£¨λ μ λ€μ μ°Ύλ μκ³ λ¦¬μ¦μΈλ° λ―Όμ½νμ€ν€μ ν© λν 곡κ°μ ν©μ κ³μ°νλ κ²μ΄κΈ° λλ¬Έμ convex hull μκ³ λ¦¬μ¦μ μ¬μ©νμ¬ κ΅¬ννλ©΄ λλ€.
μ½λμ ν΅μ¬μ μΈ λΆλΆμ https://justicehui.github.io/ioi/2019/05/21/BOJ2244/ μμ λ°μλ€.
λͺκ°μ§ λλ€μμ΄λΌλμ§ autoμ κ°μ κ²λ€μ΄ λμκ² μ΄λ €μμ forλ¬ΈμΌλ‘ λ°κΎΈκ±°λ μ£Όμμ λ¬μμ μ§νν΄λ³΄μλ€. κΈ°μ‘΄μ λ€λ₯Έκ³³μμ μ°Ύμμ convex hullμ ꡬνν΄μ λ£μ΄λ³΄μλλ° μμ λ₯Ό λ£μ΄λ³΄μμ λλ μΆλ ₯ κ²°κ³Όκ° μλμμλ€. νμ§λ§ λ°±μ€μμ ν μ€νΈ ν λλ§λ€
μ΄λ¬ν λ°νμ μλ¬λ μΆλ ₯ μ΄κ³Όκ° λμμ λκ° λ¬Έμ μΈμ§ λͺ°λΌμ λ€λ₯Έ λΆ λΈλ‘κ·Έλ₯Ό λ³΄κ³ μ°Έκ³ ν΄μ ν΄λ³΄μλλ° μλ§ sortingνλ κ³Όμ μμ μλ¬κ° λκ² μλκΉ μΆλ€..
convex hull μκ³ λ¦¬μ¦μ λν κΈμ https://kbw1101.tistory.com/50 μ¬κΈ° λΈλ‘κ·Έμ μμ£Ό μΉμ νκ² μ€λͺ λμ΄ μλ€.
*μ΄ λ°©λ²λ§μ΄ λ§λ μ λ΅μ μλλλ€.
ν¨μ¬ μ’κ³ λΉ λ₯Έ λ€λ₯Έ μκ³ λ¦¬μ¦μ ꡬνν μ μμ΅λλ€.
μκ³ λ¦¬μ¦ κ³΅λΆνμλ λΆλ€ νμ΄ν ! '0'/*
'μκ³ λ¦¬μ¦ > BaekJoon' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C++] BOJ 6087λ²: λ μ΄μ ν΅μ (0) | 2022.06.28 |
---|---|
[C++] BOJ 2749λ²: νΌλ³΄λμΉ μ 3 (0) | 2022.06.20 |
[C++] BOJ 2933λ²: λ―Έλ€λ (0) | 2022.06.13 |
[C++] BOJ 1655λ²: κ°μ΄λ°λ₯Ό λ§ν΄μ (0) | 2022.06.08 |
[C++] BOJ 3190λ²: λ± (0) | 2022.03.25 |