Cod sursa(job #1581332)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 26 ianuarie 2016 19:08:13
Problema Infasuratoare convexa Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.28 kb
/* O(N^4) algorithm */

#include <fstream>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <iomanip>
using namespace std;

const double eps = 0.0000000001;

double getDet3(pair<double, double> p1, pair<double, double> p2, pair<double, double> p3)
{
    return p1.first * (p2.second - p3.second) +
            p2.first * (p3.second - p1.second) +
            p3.first * (p1.second - p2.second);
}

bool isExtremePoint(int pointIndex, pair<double, double> points[], int N)
{
    int i, j, k;
    double triangleArea, area1, area2, area3;
    for (i = 0; i < N - 2; i++)
        if (i != pointIndex)
            for (j = i + 1; j < N - 1; j++)
                if (j != pointIndex)
                    for (k = j + 1; k < N; k++)
                        if (k != pointIndex)
                        {
                            triangleArea = abs(getDet3(points[i], points[j], points[k]));
                            area1 = abs(getDet3(points[pointIndex], points[i], points[j]));
                            area2 = abs(getDet3(points[pointIndex], points[i], points[k]));
                            area3 = abs(getDet3(points[pointIndex], points[k], points[j]));
                            if (triangleArea == area1 + area2 + area3)
                                return false;
                        }

    return true;
}

int main()
{
    int N, i;
    ifstream f("infasuratoare.in");
    f >> N;

    pair<double, double> points[N];
    vector<pair<double, double>> sol;
    for (i = 0; i < N; i++)
        f >> points[i].first >> points[i].second;
    f.close();

    for (i = 0; i < N; i++)
        if (isExtremePoint(i, points, N))
            sol.push_back(points[i]);

    pair<double, double> center(0, 0);
    for (i = 0; i < sol.size(); i++)
    {
        center.first += sol[i].first;
        center.second += sol[i].second;
    }
    center.first /= sol.size();
    center.second /= sol.size();

    sort(sol.begin(), sol.end(), [&] (const pair<double, double> &a, const pair<double, double> &b) {
            return getDet3(center, a, b) > 0;
         });

    ofstream g("infasuratoare.out");
    g << sol.size() << '\n';
    for (i = 0; i < sol.size(); i++)
        g << fixed << setprecision(12) << sol[i].first << ' ' << sol[i].second << '\n';
    g.close();
    return 0;
}