Cod sursa(job #478333)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 18 august 2010 10:01:01
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#define nmax 3502

using namespace std;

const char iname[] = "cutii.in";
const char oname[] = "cutii.out";

ifstream fin(iname);
ofstream fout(oname);

struct box
{
	int x, y, z;
}; 

box cutie[nmax];
int N, T, i, j;
int AIB[nmax][nmax];

struct cmp
{
	bool operator()(const box &a, const box &b)const
	{
		if(a.z <= b.z)
			return 1;
		return 0;
	}
};

void update(int first, int second, int valoare)
{	
	int p = first;
	int q = second;
	for(;p <= N; p += p & -p)
		for(; q <= N; q += q & -q)
			if(valoare > AIB[p][q])
				AIB[p][q] = valoare;
}

int query(int first, int second)
{	
	int p = first;
	int q = second;
	int mx = 0;
	for(;p >= 1; p -= p & -p)
		for(;q >= 1; q -= q & -q)
			if(AIB[p][q] > mx)
				mx = AIB[p][q];
	return mx;
}
			
void erase(int first, int second)
{
	int p = first;
	int q = second;
	for(; p <= N; p += p & -p)
		for(; q <= N; q += q & - q)
			AIB[p][q] = 0;
}

int main()
{
	fin >> N >> T;
	for(i = 1; i <= T; i ++)
	{
		for(j = 1; j <= N; j ++)
			fin >> cutie[j].x >> cutie[j].y >> cutie[j].z;
		sort(cutie + 1, cutie + N + 1, cmp());
		int maxx = 0;
		int sol = 0;
		for(j = 1; j <= N; j ++)
		{
			sol = query(cutie[j].x, cutie[j].y);
			update(cutie[j].x, cutie[j].y, sol + 1);
			if(sol + 1 > maxx)
				maxx = sol + 1;
		}
		fout << maxx << "\n";
		for(j = 1; j <= N; j ++)
			erase(cutie[j].x, cutie[j].y);
			
	}
	return 0;
}