Cod sursa(job #2898460)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 6 mai 2022 20:11:03
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
/// Preset de infoarena
#include <fstream>
#include <algorithm>

using namespace std;

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

const int maxN = 3505;
int n, aib[maxN][maxN];
struct cutie {
    int x, y, z;
    bool operator < (const cutie &other) const
    {
        return x < other.x;
    }
}v[maxN];

void update(int lin, int col, int val)
{
    for(int i = lin; i <= n; i += (i & (-i)))
        for(int j = col; j <= n; j += (j & (-j)))
            aib[i][j] = max(aib[i][j], val);
}

void reset(int lin, int col)
{
    for(int i = lin; i <= n; i += (i & (-i)))
        for(int j = col; j <= n; j += (j & (-j)))
            aib[i][j] = 0;
}

int query(int lin, int col)
{
    int aux = 0;
    for(int i = lin; i > 0; i -= (i & (-i)))
        for(int j = col; j > 0; j -= (j & (-j)))
            aux = max(aux, aib[i][j]);
    return aux;
}

void solve()
{
    int ans = 0;
    for(int i = 1; i <= n; i++)
        fin >> v[i].x >> v[i].y >> v[i].z;
    sort(v + 1, v + n + 1);
    for(int i = 1; i <= n; i++)
    {
        int val = query(v[i].y - 1, v[i].z - 1);
        val++;
        update(v[i].y, v[i].z, val);
        ans = max(ans, val);
    }
    fout << ans << '\n';
    for(int i = 1; i <= n; i++)
        reset(v[i].y, v[i].z);
}

int main()
{
    int nr_teste;
    fin >> n >> nr_teste;
    while(nr_teste--)
        solve();
    return 0;
}