Cod sursa(job #2530809)

Utilizator robert.barbu27robert barbu robert.barbu27 Data 25 ianuarie 2020 12:32:41
Problema Gradina Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.87 kb

#include <algorithm>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
ifstream f("gradina.in");
ofstream g("gradina.out");
struct punct
{
    int x,y,poz;
};
punct v[1000];
punct a[1005],b[1005];
char afis[1005];
char aux[1005];
int n;
bool cmp(punct a,punct b)
{
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long sp(punct a,punct b,punct c)
{
    return 1LL*(b.x-a.x)*(c.y-a.y)-1LL*(c.x-a.x)*(b.y-a.y);
}
bool checkconvex(punct pct[],int lungime)
{
    int cntstiva=0;
    punct stiva[100005];
    for(int i=1; i<=lungime; i++)
    {
        while(cntstiva>1&&sp(stiva[cntstiva-1],stiva[cntstiva],pct[i])<0)
        {
            cntstiva--;
        }
        stiva[++cntstiva]=pct[i];
    }
    int ccnt=cntstiva;
    for(int i=lungime-1; i>=1; i--)
    {
        while(cntstiva>ccnt&&sp(stiva[cntstiva-1],stiva[cntstiva],pct[i])<0)
        {
            cntstiva--;
        }
        stiva[++cntstiva]=pct[i];
    }
    cntstiva--;
    if(cntstiva!=lungime) return false;
    if(sp(stiva[cntstiva-1],stiva[cntstiva],pct[1])<0) return false;
    return true;

}
long long arie(punct pct[],int lungime)
{

    long long sol=0;
    pct[lungime+1]=pct[1];

    for(int i=1; i<=lungime; i++)
    {
        sol=sol+(pct[i].x*pct[i+1].y-pct[i].y*pct[i+1].x);


    }


    //sol=sol+sp(O,pct[lungime],pct[1]);

    return abs(sol);
}
bool mystringcompare(char s1[],char s2[])
{
    for(int i=1;i<=n;i++)
    {
        if(s1[i]>s2[i]) return 0;
    }
    return 1;
}


int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
    {
        f>>v[i].x>>v[i].y;
        v[i].poz=i;


    }
    a[1]=v[1];
    a[2]=v[2];
    a[3]=v[5];
    a[4]=v[6];
    b[1]=v[3];
    b[2]=v[4];
    b[3]=v[7];


    sort(v+1,v+n+1,cmp);
    checkconvex(v,n);
    long long solmin=(1LL<<50);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            int cnta=0,cntb=0;
            if(i==j) continue;
            for(int k=1; k<=n; k++)
            {
                if(k==i)
                {
                    a[++cnta]=v[k];
                    aux[v[k].poz]='I';
                    continue;
                }
                if(k==j)
                {
                    b[++cntb]=v[k];
                    aux[v[k].poz]='V';
                                  continue;
                }
                if((sp(v[i],v[j],v[k])<0)==(i>j)) {a[++cnta]=v[k];aux[v[k].poz]='I';}
                else {b[++cntb]=v[k];aux[v[k].poz]='V';}
            }
            if(cnta>=3&&cntb>=3)
            {
                if(checkconvex(a,cnta)&&checkconvex(b,cntb))
                {
                    long long dif=abs(arie(a,cnta)-arie(b,cntb));
                    {
                        if(dif<=solmin)
                        {
                            if(dif<solmin)
                            {
                                solmin=dif;
                                for(int i=1; i<=n; i++)
                                {
                                    afis[i]=aux[i];
                                }

                            }
                            else
                            {
                                if(mystringcompare(aux,afis)==1)
                                {
                                    for(int i=1; i<=n; i++)
                                    {
                                        afis[i]=aux[i];
                                    }
                                }
                            }
                        }


                    }
                }
            }
            }
        }

    g<<solmin/2;
    if(solmin%2==0) g<<".0";
    else g<<".5";
    g<<'\n';
    for(int i=1;i<=n;i++)
    {
        g<<afis[i];
    }

}