Cod sursa(job #478340)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 18 august 2010 10:14:41
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 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 q = second;
	for(;first <= N; first += first ^ (first - 1) & first)
		for(q = second ; q <= N; q += q ^ (q - 1) & q)
			if(valoare > AIB[first][q])
				AIB[first][q] = valoare;
}

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