Cod sursa(job #1959429)

Utilizator the.manIon Man the.man Data 9 aprilie 2017 14:44:55
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

struct Cutie
{
    int x, y, z;

    bool operator < (const Cutie &c) const
    {
        return z < c.z;
    }
};

int n,t;
int aib[3501][3501];  // aib[x][y] - nr max de cutii cu _x < x si _y < y

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

int Query(int x, int y)
{
    int ans = 0;
   for (int i = x; i >= 1; i -= i & -i)
		for (int j = y; j >= 1; j -= j & -j)
            ans = max(ans, aib[i][j]);
    return ans;
}

void Reset(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;
}

int Solve(int n)
{
    vector<Cutie> C;
    int x, y, z;

    for (int i = 1; i <= n; i++)
    {
        fin >> x >> y >> z;
        C.push_back({x, y, z});
    }
    sort(C.begin(), C.end());

    int ans = 0;
    for (const auto& c : C)
    {
        int d = Query(c.x - 1, c.y - 1) + 1;
        Update(c.x, c.y, d);
        ans = max(ans, d);
    }

    for (const auto& c : C) Reset(c.x, c.y);

    return ans;
}

int main()
{
    fin >> n >> t;

    for(int i = 1; i <= t; i++)
        fout << Solve(n) << '\n';

    return 0;
}