Cod sursa(job #741762)

Utilizator deneoAdrian Craciun deneo Data 26 aprilie 2012 22:12:51
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;

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

#define MAXN 3501

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

cutie v[MAXN];
int n, t, arb[MAXN][MAXN];

bool cmp(cutie a, cutie b) {
    return a.x < b.x;
}

void reset(int poz) {
    memset(arb[poz], 0, sizeof(arb[poz]));
}

void update(int x, int y, int val) {
    int i, j;
    for (i = x; i <= n; i += i & (-i))
        for (j = y; j <= n; j += j & (-j))
            arb[i][j] = max(val, arb[i][j]);
}

int query(int x, int y) {
    int i, j, rez = 0;
    for (i = x; i > 0; i -= i & (-i))
        for (j = y; j > 0; j -= j & (-j))
            rez = max (rez, arb[i][j]);
    return rez;
}

int main() {
    int i, rez, rezmax = -1;
    fin >> n >> t;
    while(t --) {
        for (i = 1; i <= n; ++i)
            reset(i);
        for (i = 1; i <= n; ++i)
            fin >> v[i].x >> v[i].y >> v[i].z;
        sort(v + 1, v + n + 1, cmp);
        rezmax = -1;
        for (i = 1; i <= n; ++i) {
            rez = query(v[i].y - 1, v[i].z - 1) + 1;
            update(v[i].y, v[i].z, rez);
            if (rezmax < rez)
                rezmax = rez;
        }
        fout << rezmax << "\n";
    }
    fout.close();
    return 0;
}