Cod sursa(job #1581410)

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

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

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();

    pair<double, double> center = points[0];
    for (i = 0; i < N; i++)
    {
        if (isExtremePoint(i, points, N))
            sol.push_back(points[i]);
        if (points[i].second < center.second)
            center = points[i];
        else if (points[i].second == center.second && points[i].first < center.first)
            center = points[i];
    }

    sort(sol.begin(), sol.end(), [&] (const pair<double, double> &a, const pair<double, double> &b) {
            /* TO-DO Why doesn't it work when center is barycenter */
            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;
}