안녕하세요. 오늘은 거리를 구해볼 거예요.
https://www.acmicpc.net/problem/9240
그냥 이거 쓰면 됩니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define ll long long
#define pii pair <double,double>
using namespace std;
ll CCW(pii A, pii B, pii C)
{
double ax = A.first, ay = A.second, bx = B.first, by = B.second, cx = C.first, cy = C.second;
double ccw = ax * (by - cy) + bx * (cy - ay) + cx * (ay - by);
if (ccw < 0) return -1;
if (ccw == 0) return 0;
if (ccw > 0) return 1;
}
ll dist(pii A, pii B)
{
return (A.first - B.first) * (A.first - B.first) + (A.second - B.second) * (A.second - B.second);
}
bool ycmp(pii A, pii B)
{
if (A.second != B.second) return A.second < B.second;
return A.first < B.first;
}
pii low_dot;
bool cmp(pii A, pii B)
{
if (CCW(low_dot, A, B) > 0) return true;
if (CCW(low_dot, A, B) == 0) return dist(low_dot, A) < dist(low_dot, B);
return false;
}
vector <pii> make_convex(vector <pii> v)
{
ll N = v.size();
sort(v.begin(), v.end(), ycmp);
low_dot = v[0];
sort(v.begin() + 1, v.end(), cmp);
ll stk[101010] = { 0 }, top = 2;
stk[0] = 0; stk[1] = 1;
for (int i = 2; i < N; i++)
{
while (top >= 2 && CCW(v[i], v[stk[top - 2]], v[stk[top - 1]]) <= 0)
top--;
stk[top++] = i;
}
vector <pii> ans;
for (int i = 0; i < top; i++)
ans.push_back(v[stk[i]]);
return ans;
}
double RoCal(vector <pii> v) //로캘
{
ll N = v.size();
ll i, next_i, j = 0, next_j;
double ans = 0;
for (i = 0; i < N; i++)
{
next_i = (i + 1) % N;
while (true)
{
ans = max(ans, sqrt(dist(v[i], v[j])));
next_j = (j + 1) % N;
pii v1, v2; //기울기
v1.first = v[i].first - v[next_i].first;
v1.second = v[i].second - v[next_i].second;
v2.first = v[j].first - v[next_j].first;
v2.second = v[j].second - v[next_j].second;
if (CCW({ 0,0 }, v1, v2) > 0)
j = next_j;
else
break;
}
ans = max(ans, sqrt(dist(v[i], v[j])));
}
return ans;
}
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int N, i, a, b;
vector <pii> v;
cin >> N;
for (i = 0; i < N; i++)
{
cin >> a >> b;
v.push_back({ a,b });
}
cout << fixed;
cout.precision(10);
cout << RoCal(make_convex(v));
}
감사합니다.