Cod sursa(job #2094660)

Utilizator gruhtenZinnenberg Gruhten gruhten Data 26 decembrie 2017 12:54:38
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <cstdio>
#define maxn 3500

FILE *in = fopen("cutii.in","r"), *out = fopen("cutii.out","w");

struct cutie
{
    int x, y, z;
};

int n, t;
cutie a[maxn];

int partition(int top, int bottom)
{
     cutie x = a[top];
     int i = top - 1;
     int j = bottom + 1;
     cutie temp;
     do
     {
        do
        {
            --j;
        } while ( x.x < a[j].x );

        do
        {
            ++i;
        } while ( x.x > a[i].x );

        if ( i < j )
        {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
     } while ( i < j );

     return j;
}

void qs(int top, int bottom)
{
    int middle;
    if ( top < bottom )
    {
        middle = partition(top, bottom);
        qs(top, middle);
        qs(middle+1, bottom);
    }
}

int lis()
{
    int max = 0;
    int l[maxn];

    for ( int i = 0; i < n; ++i )
    {
        l[i] = 1;
        for ( int j = 0; j < i; ++j )
            if ( l[i] < l[j] + 1 and a[j].y < a[i].y and a[j].z < a[i].z )
                l[i] = l[j] + 1;

        if ( l[i] > max )
            max = l[i];
    }

    return max;
}

int main()
{
    fscanf(in, "%d %d", &n, &t);

    while ( t )
    {
        for ( int i = 0; i < n; ++i )
            fscanf(in, "%d %d %d", &a[i].x, &a[i].y, &a[i].z);
        qs(0, n-1);
        fprintf(out, "%d\n", lis());
        --t;
    }

    return 0;
}