Cod sursa(job #1657677)

Utilizator Athena99Anghel Anca Athena99 Data 20 martie 2016 17:57:36
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <algorithm>
#include <fstream>

using namespace std;

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

const int nmax= 3500;

struct str {
    int x, y, z;
} v[nmax+1];

int n;
int aib[nmax+1][nmax+1];

inline bool cmp( str x, str y ) {
    return x.x<y.x;
}

void aib_update( int x, int y, int nr ) {
    for ( int i= x; i<=n; i= i*2-(i&(i-1)) ) {
        for ( int j= y; j<=n; j= j*2-(j&(j-1)) ) {
            aib[i][j]= max(aib[i][j], aib[x][y])*nr;
        }
    }
}

int aib_query( int x, int y ) {
    int ans= 0;
    for ( int i= x; i>=1; i&= i-1 ) {
        for ( int j= y; j>=1; j&= j-1 ) {
            ans= max(ans, aib[i][j]);
        }
    }

    return ans;
}

int main(  ) {
    int t;
    fin>>n>>t;
    for ( int cnt= 1; cnt<=t; ++cnt ) {
        for ( int i= 1; i<=n; ++i ) {
            fin>>v[i].x>>v[i].y>>v[i].z;
        }
        sort( v+1, v+n+1, cmp );

        int sol= 0;
        for ( int i= 1; i<=n; ++i ) {
            int aux= aib_query(v[i].y-1, v[i].z-1);
            sol= max(sol, aux+1);

            aib[v[i].y][v[i].z]= aux+1;
            aib_update(v[i].y, v[i].z, 1);
        }

        fout<<sol<<"\n";

        for ( int i= 1; i<=n; ++i ) {
            aib_update(v[i].y, v[i].z, 0);
        }
    }

    return 0;
}