Cod sursa(job #1654483)

Utilizator DanielRusuDaniel Rusu DanielRusu Data 17 martie 2016 08:35:13
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

#define DIM 3503

const int INF = 100000;
int dp[DIM], hop;

struct cutie {
    int a, b, c;
} V[DIM], ram[DIM];

bool cmp(cutie x, cutie y);
int bin_search(cutie nr);

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

    int N, T;

    scanf("%d %d\n", &N, &T);

    while(T) {
        --T;

        for(int i = 1; i <= N; ++i) {
            scanf("%d %d %d\n", &V[i].a, &V[i].b, &V[i].c);
        }

        sort(V + 1, V + 1 + N, cmp);

        memset(dp, INF, sizeof(dp));
        dp[N] = 1;

        hop = 1;
        ram[1] = V[N];

        for(int i = N - 1; i > 0; --i) {
            int pos = bin_search(V[i]);

            if(pos == hop) {
                ++hop;
                ram[pos + 1] = V[i];
            }
        }

        cout << hop << '\n';
    }

    return 0;
}

bool cmp(cutie x, cutie y) {
    if(x.a == y.a) {
        if(x.b == y.b) {
            return x.c < y.c;
        }

        return x.b < y.b;
    }

    return x.a < y.a;
}

int bin_search(cutie nr) {
    int put = 1;

    while(put <= hop) {
        put <<= 1;
    }

    put >>= 1;

    int pos = 0;

    while(put) {
        if(pos + put <= hop) {
            if(ram[pos + put].a > nr.a && ram[pos + put].b > nr.b && ram[pos + put].c > nr.c) {
                pos += put;
            }
        }

        put >>= 1;
    }

    return pos;
}