Pagini recente » Cod sursa (job #3309780) | ..::Hyper Kid Designer Profile::.. | Cod sursa (job #3358002) | Cod sursa (job #3357793) | Cod sursa (job #3357896)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
#include <iomanip>
struct Point {
Point() = default;
Point(double x, double y) : x(x), y(y) {}
double x = 0, y = 0;
inline friend std::istream &operator>>(std::istream &istream, Point &point) {
return istream >> point.x >> point.y;
}
inline friend std::ostream &operator<<(std::ostream &ostream, const Point &p) {
return ostream << p.x << " " << p.y;
}
static double cross_product(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);
}
};
bool check(std::stack<Point> &stack, const Point &cnd) {
Point p1 = stack.top();
stack.pop();
Point p2 = stack.top();
stack.push(p1);
return Point::cross_product(p2, p1, cnd) > 0;
}
int main() {
std::ifstream input("infasuratoare.in");
std::ofstream output("infasuratoare.out");
int n;
input >> n;
std::vector<Point> points(n);
for (int i = 0; i < n; ++i) {
input >> points[i];
}
int pos = 0;
for (int i = 1; i < n; ++i) {
if (points[i].x < points[pos].x || (points[i].x == points[pos].x && points[i].y < points[pos].y)) {
pos = i;
}
}
std::swap(points[0], points[pos]);
Point ref = points[0];
std::sort(points.begin() + 1, points.end(), [ref](const Point &lhs, const Point &rhs) {
double cp = Point::cross_product(ref, lhs, rhs);
if (cp == 0) {
double dx1 = lhs.x - ref.x;
double dy1 = lhs.y - ref.y;
double dx2 = rhs.x - ref.x;
double dy2 = rhs.y - ref.y;
return (dx1 * dx1 + dy1 * dy1) < (dx2 * dx2 + dy2 * dy2);
}
return cp > 0;
});
std::stack<Point> stack;
stack.push(points[0]);
stack.push(points[1]);
for (int i = 2; i < n; ++i) {
while (stack.size() >= 2) {
Point p1 = stack.top();
stack.pop();
Point p2 = stack.top();
if (Point::cross_product(p2, p1, points[i]) > 0) {
stack.push(p1);
break;
}
}
stack.push(points[i]);
}
output << stack.size() << "\n";
output << std::setprecision(12) << std::fixed;
std::vector<Point> result;
while (!stack.empty()) {
result.push_back(stack.top());
stack.pop();
}
std::reverse(result.begin(), result.end());
for (const auto &p : result) {
output << p << '\n';
}
return 0;
}