Cod sursa(job #1527650)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 18 noiembrie 2015 15:39:27
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

const int MAX_N = 3500;

struct Box {
    int x;
    int y;
    int z;
};

int n;
int AIB[1 + MAX_N][1 + MAX_N];
Box B[1 + MAX_N];

int lsb(int x) {
    return x & (-x);
}

void reset() {
    int i, j;
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= n; j++) {
            AIB[i][j] = 0;
        }
    }
}

void update(int x, int y, int val) {
    int i, j;
    for(i = x; i; i -= lsb(i)) {
        for(j = y; j; j -= lsb(j)) {
            AIB[i][j] = max(AIB[i][j], val);
        }
    }
}

int query(int x, int y) {
    int i, j, ans = 0;
    for(i = x; i <= n; i += lsb(i)) {
        for(j = y; j <= n; j += lsb(j)) {
            ans = max(ans, AIB[i][j]);
        }
    }
    return ans;
}

bool boxSort(Box A, Box B) {
    return A.z > B.z;
}

int main() {
    int t, i, ans, val;

    in >> n >> t;
    while(t--) {
        for(i = 1; i <= n; i++) {
            in >> B[i].x >> B[i].y >> B[i].z;
        }
        sort(B + 1, B + n + 1, boxSort);
        ans = 1;
        reset();
        update(B[1].x, B[1].y, 1);
        for(i = 2; i <= n; i++) {
            val = query(B[i].x, B[i].y) + 1;
            ans = max(ans, val);
            update(B[i].x, B[i].y, val);
        }

        out << ans << '\n';
    }

    return 0;
}