Cod sursa(job #3004769)

Utilizator Vincent47David Malutan Vincent47 Data 16 martie 2023 16:29:39
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 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;
    }
};
const int dim = 3501;
int aib[dim + 1][dim + 1];
void reset(int cx, int cy)
{
    for (int x = cx; x <= dim; x += x & -x)
        for (int y = cy; y <= dim; y += y & -y)
        aib[x][y] = 0;
}

void update(int cx, int cy, int val)
{
    for (int x = cx; x <= dim; x += x & -x)
        for (int y = cy; y <= dim; y += y & -y)
        aib[x][y] = max (aib[x][y], val);
}

int query(int cx, int cy)
{
    int ans = 0;
    for (int x = cx; x > 0; x -= x & -x)
        for (int y = cy; y > 0; y -= y & -y)
        ans = max(ans, aib[x][y]);
    return ans;
}

int solve(int n)
{
    vector<Cutie>C;
    int x, y, z;
    for (int  i = 1; i <= n; ++ i)
    {
        fin >> x >> y >> z;
        Cutie a;
        a.x = x;
        a.y = y;
        a.z = z;
        C.push_back(a);
    }
    sort(C.begin(), C.end());
    int answer = 0;
    for (const auto c : C)
    {
        int d = query(c.x - 1, c.y - 1) + 1;
        update(c.x, c.y, d);
        answer = max(answer, d);
    }

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

    return answer;
}

int main()
{
    int n, t;
    fin >> n >> t;
    for (int i = 1; i <= t; ++i)
     fout << solve(n) << '\n';

    return 0;
}