Cod sursa(job #1810039)

Utilizator ancabdBadiu Anca ancabd Data 19 noiembrie 2016 15:44:10
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

#define MAX 3500

struct cutie
{
    int nrs, x, y;
    bool operator < (const cutie &c)const
    {
        return nrs < c.nrs;
    }
};

vector<cutie> a;
int n, t, aib[MAX + 1][MAX + 1];

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

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

    return rez;
}

void clean(int x, int y)
{
    for (int i = x; i <= n; i += (i&-i))
        for (int j = y; j <= n; j += (j & -j))
            aib[i][j] = 0;
}
void Solve()
{
    int rez = 0;

    for (int i = 1; i <= n; i++)fin >> a[i].nrs >> a[i].x >> a[i].y;
    sort(a.begin(), a.end());

    for (int i = 1; i <= n; i++)
    {
        int d = query(a[i].x, a[i].y);
        update(a[i].x, a[i].y, d + 1);
        rez = max(rez, d + 1);
    }

    for (int i = 1; i <= n; i++)clean(a[i].x, a[i].y);

    fout << rez << '\n';
}
int main()
{
    fin >> n >> t;

    a.resize(n + 1);
    for (int i = 1; i <= t; i++)Solve();
    return 0;
}