Cod sursa(job #2411197)

Utilizator CezarTDTodirisca Cezar CezarTD Data 20 aprilie 2019 14:04:07
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.27 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("colorfulconflict.in");
ofstream fout("colorfulconflict.out");

const int N=1010;

int n,A[N][N],B[N][N];
int sus[N*N],jos[N*N],dr[N*N],st[N*N],mjos,msus,mst,mdr;
struct colt
{
    int x,y;
} c1,c2,c3,c4;
int maxx;

bool overlap(colt a1,colt a2,colt b1,colt b2)
{
    if(a1.x>b2.x || b1.x>a2.x)
        return false;
    if(a1.y<b2.y || b1.y<a2.y)
        return false;
    return true;
}

int main()
{
    fin>>n;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            fin>>A[i][j];
            if(A[i][j]>maxx)
                maxx=A[i][j];
            if(!st[A[i][j]])
            {
                st[A[i][j]]=j;
                dr[A[i][j]]=j;
                sus[A[i][j]]=i;
                jos[A[i][j]]=i;
            }
            else
            {
                if(j<st[A[i][j]])
                    st[A[i][j]]=j;
                if(j>dr[A[i][j]])
                    dr[A[i][j]]=j;
                if(i<sus[A[i][j]])
                    sus[A[i][j]]=i;
                if(i>jos[A[i][j]])
                    jos[A[i][j]]=i;
            }
        }
    }
    for(int i=1; i<maxx-1; i++)
    {
        if(st[i])
        {
            for(int j=i+1; j<maxx; j++)
            {
                if(st[j])
                {
                    c1.x=st[i];
                    c1.y=sus[i];
                    c2.x=dr[i];
                    c2.y=jos[i];
                    c3.x=st[j];
                    c3.y=sus[j];
                    c4.x=dr[j];
                    c4.y=jos[j];
                    if(!overlap(c1,c2,c3,c4) && !overlap(c3,c4,c1,c2))
                    {
                        int ok=1;
                        for(int k=j+1; k<=maxx; k++)
                        {
                            if(st[k])
                            {
                                c3.x=st[k];
                                c3.y=sus[k];
                                c4.x=dr[k];
                                c4.y=jos[k];
                                if(overlap(c1,c2,c3,c4) || overlap(c3,c4,c1,c2))
                                {
                                    ok=0;
                                    break;
                                }
                                c1.x=st[j];
                                c1.y=sus[j];
                                c2.x=dr[j];
                                c2.y=jos[j];
                                c3.x=st[k];
                                c3.y=sus[k];
                                c4.x=dr[k];
                                c4.y=jos[k];
                                if(overlap(c1,c2,c3,c4) || overlap(c3,c4,c1,c2))
                                {
                                    ok=0;
                                    break;
                                }
                                if(ok)
                                {
                                    fout<<i<<' '<<j<<' '<<k;
                                    return 0;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    fout<<-1;
    return 0;
}