-
Notifications
You must be signed in to change notification settings - Fork 1
21-froglike6 #82
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
21-froglike6 #82
Conversation
|
๋ช์ฃผ์ ์ ๋ชปํ์๋ ๋ฌธ์ ์ธ๋ ๋ค์ ๋์ ํด๋ด์ผ๊ฒ ๋ค์ ใ ใ |
dohyeondol1
left a comment
There was a problem hiding this 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;
}|
์ ๋ ์๋์ฝ๋๋ฅผ ๋ณด๋ฉด์ ์ฝ๋๋ฅผ ์ง๋ณด์์ต๋๋ค. 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;
} |
๐ ๋ฌธ์ ๋งํฌ
Cows
โ๏ธ ์์๋ ์๊ฐ
์์ ์ ํ์๋ ๋ฌธ์ ๋ผ ํผ ์๊ฐ์ด ๊ธฐ์ต์ ๋์ง ์์ง๋ง, 1์๊ฐ ์ ๋ ๊ฑธ๋ฆฐ ๊ฒ ๊ฐ์ต๋๋ค.
โจ ์๋ ์ฝ๋
์๋ ์ฝ๋
๋๋ฌด๊ฐ ์ ์ฒ๋ผ ์ฌ๊ฒจ ์๋ค๊ณ ๊ฐ์ ํฉ์๋ค. ๋น์ฐํ ๋ค์๊ณผ ๊ฐ์ ์ํฉ์ผ ๋ ์ต๋ ๋์ด๊ฐ ๋ฉ๋๋ค.
์ด ๋ฐ๊นฅ ์ ๋ค์ ์ด๋ป๊ฒ ๊ตฌํด์ผ ํ ๊น์? ๋ฐ๋ก Convex Hull์ด๋ผ๋ ์๊ณ ๋ฆฌ์ฆ์ ํตํด ๊ตฌํ๊ฒ ๋ฉ๋๋ค. ๊ทธ ์ค์์๋ ์ ๋ Monotone chain์ด๋ผ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ต๋๋ค.
Monotone Chain ๋ฐฉ๋ฒ์ ์ 3๊ฐ๋ฅผ ์ ํํ์ฌ ์ธ์ ์ ํ ํ, ๊ทธ ๋ถํธ๋ก ๊ฐ์ฅ ๋ฐ๊นฅ์ ์๋ ์ ์ ์ ํํ์ฌ ๋ณผ๋ก๊ป์ง์$\mathcal{O}(n log n)$ ๋ง์ ์ฐพ๋ ๋ฐฉ๋ฒ์
๋๋ค.
์ด ์์ง์ด๋ ์ฌ์ง์ ๋ณด๋ฉด, ํ์ฌ ์ ํ๋ ์ ์์ ๋ค์ ์ (X๊ฐ ๊ธฐ์ค)์ ์ ํํ๊ณ ์ธ์ ์ ํ์ ๋์ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋ณํ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค. ์ธ์ ํ ๊ฐ์ด ์์๋ผ๋ฉด ์ค๋ชฉ๋ค๊ฐํ์ด ๋๊ธฐ ๋๋ฌธ์, ๊ทธ ๊ฐ์ ์ ๊ฑฐํ๊ณ ๋ค๋ฅธ ์ ์ ์ ํํ์ฌ ์ด ๊ณผ์ ์ ๋ฐ๋ณตํ๊ฒ ๋ฉ๋๋ค.
์ฒ์ ์์ํ ๋ X๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๊ณ ์์ํ๊ธฐ ๋๋ฌธ์, ์คํ์ ์ฌ์ฉํ์ฌ ๊ตฌํํ๋ฉด ๋ฉ๋๋ค. ์ธ์ ์ ๊ดํ ๋ด์ฉ์ ์ด๋ฏธ #65 ์ ์ ์ค๋ช ๋์ด ์์ต๋๋ค ใ ใ ใ .
์ด ๋ฐฉ์์ผ๋ก ์๋์ชฝ ๋ถ๋ถ์ ๊ตฌํ๊ณ , ์ญ์ํ์์ผ๋ก ์์ชฝ ๋ถ๋ถ์ ๊ตฌํ์ฌ ๋์ ์ด์ด์ฃผ๋ฉด ๋ณผ๋ก๊ป์ง์ด ์์ฑ๋ฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๊ทธ ๋์ด๋ฅผ ์ ๋ฐ๋์ผ๋ก ๊ตฌํ๊ณ , 50์ผ๋ก ๋๋ ๋ชซ์ ์ถ๋ ฅํ๋ฉด ์ ๋ต์ ๋๋ค!!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ