Pagini recente » Cod sursa (job #2401595) | Cod sursa (job #2976749) | Cod sursa (job #2904119) | Cod sursa (job #1298710) | Cod sursa (job #3253045)
#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 MAX_N = 3500;
int aib[MAX_N + 10][MAX_N + 10]; // aib[x][y] - nr max de cutii cu _x < x si _y < y
// care pot fi incuibate una in alta
void Update(int cx, int cy, int val)
{
for (int x = cx; x <= MAX_N; x += x & -x)
for (int y = cy; y <= MAX_N; 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;
}
void Reset(int cx, int cy)
{
for (int x = cx; x <= MAX_N; x += x & -x)
for (int y = cy; y <= MAX_N; y += y & -y)
aib[x][y] = 0;
}
int Solve(int n)
{
vector<Cutie> C;
int x, y, z;
for (int i = 1; i <= n; i++)
{
fin >> x >> y >> z;
C.emplace_back(x, y, z);
}
sort(C.begin(), C.end()); // O(n * log2(n)) sortare dupa z
int ans = 0;
for (const auto& c : C) // O(n * log2(n) * log2(n))
{
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) // O(n * log2(n) * log2(n))
Reset(c.x, c.y);
return ans;
}
int main()
{
int n, t;
fin >> n >> t;
for(int i = 1; i <= t; i++)
fout << Solve(n) << '\n';
return 0;
}