Cod sursa(job #2556436)

Utilizator Rares31100Popa Rares Rares31100 Data 24 februarie 2020 21:35:57
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.72 kb
#include <bits/stdc++.h>
#define PII pair<int,int>
#define TIII tuple<int,int,int>
#define ULL unsigned long long
#define Lin first
#define Col second

using namespace std;

int C,n;
char mat[1001][1001];

int urm[1001][1002];
TIII stiva[1000001];int vfS;
ULL sum;

int apa[1001][1001];
int vec[1001][1001];
int cost[1001][1001];
bool viz[1002][1002];
PII coada[1000001];int last=1,vf=0;
const int dLin[]={-1,0,1,0},dCol[]={0,1,0,-1};
int lung,tot;

void create()
{
    for(int i=1;i<=n;i++)
    {
        if(mat[i][n]=='V')
            urm[i][n]=1;
        else
            urm[i][n]=-1;

        for(int j=n-1;j>=1;j--)
            if(mat[i][j]=='V')
    }
}

void Lee()
{
    for(int i=0;i<=n+1;i++)
        viz[i][0]=viz[i][n+1]=viz[0][i]=viz[n+1][i]=1;

    while(last<=vf)
    {
        int i=coada[last].Lin;
        int j=coada[last].Col;
        last++;

        if(cost[i][j]==1)
            vec[i][j]=1;

        for(int k=0;k<=3;k++)
        {
            int iVec=i+dLin[k];
            int jVec=j+dCol[k];

            if(!viz[iVec][jVec])
            {
                cost[iVec][jVec]=cost[i][j]+1;
                viz[iVec][jVec]=1;
                coada[++vf]={iVec,jVec};
            }
        }
    }
}

void SumePart()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            cost[i][j]+=cost[i-1][j]+cost[i][j-1]-cost[i-1][j-1];
            apa[i][j]+=apa[i-1][j]+apa[i][j-1]-apa[i-1][j-1];
            vec[i][j]+=vec[i-1][j]+vec[i][j-1]-vec[i-1][j-1];
        }
}

bool found(int l)
{
    bool ok=0;

    int Apa,Vec,Cost;
    for(int i=l;i<=n;i++)
        for(int j=l;j<=n;j++)
        {
            Apa=apa[i][j]-apa[i-l][j]-apa[i][j-l]+apa[i-l][j-l];
            Vec=vec[i][j]-vec[i-l][j]-vec[i][j-l]+vec[i-l][j-l];
            Cost=cost[i][j]-cost[i-l][j]-cost[i][j-l]+cost[i-l][j-l];

            if(!Apa && Vec)
            {
                if(!ok)
                {
                    ok=1;
                    tot=Cost;
                }
                else
                    tot=min(tot,Cost);
            }
        }

    return ok;
}

int main()
{
    cin>>C>>n;

    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            cin>>mat[i][j];

            if(mat[i][j]=='A')
            {
                coada[++vf]={i,j};
                viz[i][j]=1;
                apa[i][j]=1;
            }
        }

    if(C==1)
    {
        create();
    }
    else
    {
        Lee();
        SumePart();

        for(int pas=n;pas;pas/=2)
            while(lung+pas<=n && found(lung+pas) )
                lung+=pas;

        cout<<lung*lung<<' '<<tot;
    }

    return 0;
}