Cod sursa(job #1707439)

Utilizator bleo16783FMI Bleotiu Cristian bleo16783 Data 25 mai 2016 08:15:44
Problema Reuniune Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int i,P,S,k,j,l1,l2,poz1,nr,poz,s1,s2,s3,s4,d1,d2;
bool w[4];
struct dr
{
    int x,y,a,b;
}v[4];
struct coord
{
    int val;
    int poz;
}x[7],y[7];
bool compare(coord a,coord b)
{
    return(a.val<b.val);
}
int modul(int a)
{
    if(a>0)return a;
    return (0-a);
}
int main()
{
    ifstream f("reuniune.in");
    for(i=1;i<4;++i)f>>v[i].x>>v[i].y>>v[i].a>>v[i].b;
    f.close();nr=0;
    while(w[0]<1)
    {
        //for(i=0;i<4;++i)cout<<w[i];cout<<'\n';
        poz=3;while(w[poz]>0){w[poz]=0;--poz;--nr;}
        ++nr;
        w[poz]=1;
        if(nr<2)
        {
            l1=v[poz].a-v[poz].x;
            l2=v[poz].b-v[poz].y;d1=l1*l2;d2=2*(l1+l2);
            S+=modul(d1);
            P+=modul(d2);
            //cout<<"arie si perimetru "<<S<<" "<<P<<'\n';
        }
        else
        {
            if(nr<3)
            {
                poz1=poz-1;while(w[poz1]<1)--poz1;
                //cout<<poz1<<" "<<poz<<'\n';
                x[1].val=v[poz].a;x[1].poz=poz;
                x[2].val=v[poz].x;x[2].poz=poz;
                x[3].val=v[poz1].x;x[3].poz=poz1;
                x[4].val=v[poz1].a;x[4].poz=poz1;
                y[1].val=v[poz].b;y[1].poz=poz;
                y[2].val=v[poz].y;y[2].poz=poz;
                y[3].val=v[poz1].y;y[3].poz=poz1;
                y[4].val=v[poz1].b;y[4].poz=poz1;
                sort(x+1,x+5,compare);
                sort(y+1,y+5,compare);
                s1=x[1].poz+x[2].poz;
                s2=x[3].poz+x[4].poz;
                s3=y[1].poz+y[2].poz;
                s4=y[3].poz+y[4].poz;//cout<<'\n'<<s1<<" "<<s2<<'\n';
                if(s1==s2&&s3==s4)
                {
                    l1=x[3].val-x[2].val;
                    l2=y[3].val-y[2].val;d1=l1*l2;d2=2*(l1+l2);
                    S=S-modul(d1);
                    P=P-modul(d2);
                }
                //cout<<S<<" "<<P<<'\n';
            }
            else
            {
                for(i=1;i<4;++i)
                {
                    x[(i-1)*2+1].val=v[i].x;x[(i-1)*2+1].poz=i;
                    x[i*2].val=v[i].a;x[i*2].poz=i;
                    y[(i-1)*2+1].val=v[i].y;y[(i-1)*2+1].poz=i;
                    y[i*2].val=v[i].b;y[i*2].poz=i;
                }
                sort(x+1,x+7,compare);
                sort(y+1,y+7,compare);
                s1=x[1].poz+x[2].poz+x[3].poz;
                s2=x[5].poz+x[4].poz+x[6].poz;
                s3=y[1].poz+y[2].poz+y[3].poz;
                s4=y[5].poz+y[4].poz+y[6].poz;//cout<<'\n'<<s1<<" "<<s2<<'\n';
                if(s1==s2&&s3==s4)
                {
                    l1=x[4].val-x[3].val;
                    l2=y[4].val-y[3].val;
                    S+=modul(l1*l2);
                    P+=2*modul(l1+l2);
                }
                //cout<<S<<" "<<P<<'\n';
            }
        }
    }
    ofstream g("reuniune.out");
    g<<S<<" "<<P;
    g.close();
    return 0;
}