Cod sursa(job #2529015)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 22 ianuarie 2020 20:59:05
Problema Cutii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>

using namespace std;

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

const int Nmax = 3500;

struct Box{
    int x, y, z;
}v[Nmax + 5];
int t, n, ans;
int aib[Nmax + 5][Nmax + 5];

void Read(){
    for (int i = 1; i <= n; i++)
        fin >> v[i].x >> v[i].y >> v[i].z;
}

int Compare(Box a, Box b){
    if (a.x == b.x){
        if (a.y == b.y)
            return a.z < b.z;
        return a.y < b.y;
    }
    return a.x < b.x;
}

int Search(int y, int z){
    int sol = 0;
    for (int i = y; i > 0; i -= i & (-i))
        for (int j = z; j > 0; j -= j & (-j))
            sol = max(sol, aib[i][j]);
    return sol;
}

void Update(int y, int z, int val){
    for (int i = y; i <= n; i += i & (-i))
        for (int j = z; j <= n; j += j & (-j))
            aib[i][j] = max(aib[i][j], val);
}

void Solve(){
    sort(v + 1, v + n + 1, Compare);
    for (int i = 1; i <= n; i++){
        int aux = Search(v[i].y - 1, v[i].z - 1) + 1;
        ans = max(ans, aux);
        Update(v[i].y, v[i].z, aux);
    }
}

void Print(){
    fout << ans << '\n';
}

void Reset(){
    ans = 0;
    memset(aib, 0, sizeof aib);
}

int main(){
    fin >> n >> t;
    while (t--){
        Read();
        Solve();
        Print();
        Reset();
    }
    return 0;
}