Cod sursa(job #2165072)

Utilizator AlexandruLacAlexandru Lacatusu AlexandruLac Data 13 martie 2018 11:05:44
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.56 kb
#include <bits/stdc++.h>
using namespace std;
const int maxN=5e4+4;
const int INF=0x3f3f3f3f;

struct point{
    int x,y;
};

struct square{
    //point leftUp;
    int x,y;
    int len;
}square1,square2,square3;

int n;
int row1,row2;
int col1,col2;
point v[maxN];

bool inside(square a,point b){
    return (a.x<=b.x && a.x+a.len>=b.x && a.y<=b.y && a.y+a.len>=b.y);
}

bool allCovered()
{
    for(int i=1;i<=n;i++)
        if(!inside(square1,v[i]) && !inside(square2,v[i]) && !inside(square3,v[i]))
            return false;

    return true;
}

bool othersCover()
{
    bool res=false;
    int firstRow=INF;
    int firstCol=INF;

    int lastRow=-INF;
    int lastCol=-INF;

    for(int i=1;i<=n;i++)
        if(!inside(square1,v[i])){
            firstRow=min(firstRow,v[i].x);
            lastRow=max(lastRow,v[i].x);

            firstCol=min(firstCol,v[i].y);
            lastCol=max(lastCol,v[i].y);
        }

    if(firstRow==INF)
        return true;

    square2.x=firstRow;
    square2.y=firstCol;

    square3.x=lastRow-square3.len;
    square3.y=lastCol-square3.len;

    if(allCovered())
        return true;

    square2.x=lastRow-square2.len;
    square2.y=firstCol;

    square3.x=firstRow;
    square3.y=lastCol-square3.len;

    if(allCovered())
        return true;

    return false;
}

bool good(int val)
{
    bool res=false;
    square1.len=square2.len=square3.len=val;

    square1.x=row1;
    square1.y=col1;

    if(othersCover())
        return true;

    square1.x=row1;
    square1.y=col2-square1.len;
    if(othersCover())
        return true;

    square1.x=row2-square1.len;
    square1.y=col1;
    if(othersCover())
        return true;

    square1.x=row2-square1.len;
    square1.y=col2-square1.len;
    if(othersCover())
        return true;

    return false;
}

int main()
{
    freopen("patrate.in","r",stdin);
    freopen("patrate.out","w",stdout);

    scanf("%d",&n);

    if(n==3){
        printf("0");
        return 0;
    }

    for(int i=1;i<=n;i++)
        scanf("%d %d",&v[i].x,&v[i].y);

    row1=col1=INF;
    row2=col2=-INF;

    for(int i=1;i<=n;i++){
        row1=min(row1,v[i].x);
        row2=max(row2,v[i].x);

        col1=min(col1,v[i].y);
        col2=max(col2,v[i].y);
    }

    int sol=0;
    int st=1,dr=50001;

    while(st<=dr)
    {
        int mij=(st+dr)/2;
        if(good(mij)){
            sol=mij;
            dr=mij-1;
        }
        else st=mij+1;
    }

    printf("%d",sol);

    return 0;
}