백준
백준 소스코드 [C++] 1708 볼록 껍질
Hani_Levenshtein
2020. 10. 24. 22:45
1708번: 볼록 껍질
첫째 줄에 점의 개수 N(3 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 점의 x좌표와 y좌표가 빈 칸을 사이에 두고 주어진다. 주어지는 모든 점의 좌표는 다르다고 가정해도 좋다. x
www.acmicpc.net
백준 소스코드 [C++] 1708 볼록 껍질
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
typedef long long ll;
using namespace std;
struct dot {
ll x, y;
};
vector<dot> dots;
int ccw(dot A, dot B, dot C) {
ll rot = (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);
if (rot > 0) return 1;
else if (rot < 0) return -1;
else return 0;
}
ll dist(dot a, dot b) {
return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
bool cmp(const dot& a, const dot& b) {
int val = ccw(dots[0], a, b);
if (val > 0) return true;
if (val < 0) return false;
if (dist(dots[0], a) < dist(dots[0], b)) return true;
return false;
}
vector<dot> graham() {
vector<dot> s;
for (int i = 0; i < dots.size(); i++) {
while (2 <= s.size() && ccw(s[s.size() - 2], s[s.size() - 1], dots[i]) <= 0)
s.pop_back();
s.push_back(dots[i]);
}
return s;
}
void convexhull() {
int temp = 0;
for (int i = 1;i < dots.size();i++)
if (dots[i].y < dots[temp].y || (dots[i].y == dots[temp].y && dots[i].x < dots[temp].x))
temp = i;
swap(dots[temp], dots[0]);
sort(dots.begin() + 1, dots.end(), cmp);
vector<dot> res = graham();
cout << res.size();
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
dots.resize(n);
for (int i = 0; i < n; i++)
cin >> dots[i].x >> dots[i].y;
convexhull();
return 0;
}