Cod sursa(job #2224953)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 25 iulie 2018 16:43:15
Problema Aria Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

const string IN_FILE = "aria.in";
const string OUT_FILE = "aria.out";

struct Point {
    int x, y;
};

inline int64_t det(const Point& a, const Point& b, const Point& c) {
    return 1LL * (b.x - a.x) * (c.y - a.y) - 1LL * (b.y - a.y) * (c.x - a.x);
}

double computeArea(const vector<Point>& polygon) {
    const int n = int(polygon.size());
    const Point origin = {0, 0};
    int64_t area = 0;
    for (int i = 0; i < n; i++) {
        area += det(origin, polygon[i], polygon[(i + 1) % n]);
    }
    return 0.5 * abs(area);
}

vector<Point> readPolygon() {
    ifstream in(IN_FILE);
    int n;
    in >> n;
    auto polygon = vector<Point>(n);
    for (int i = 0; i < n; i++) {
        in >> polygon[i].x >> polygon[i].y;
    }
    in.close();
    return polygon;
}

void writeArea(const double area) {
    ofstream out(OUT_FILE);
    out << fixed << setprecision(5) << area << "\n";
    out.close();
}

int main() {
    const auto polygon = readPolygon();
    const double area = computeArea(polygon);
    writeArea(area);
    return 0;
}