Cod sursa(job #805055)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 30 octombrie 2012 20:57:56
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.15 kb

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>

const int MAX_SIZE(3500);
const int DIMENSIONS(3);

int n, t;

int boxes [MAX_SIZE] [DIMENSIONS];
int sorted [MAX_SIZE];
int substring [MAX_SIZE];

inline void read (void)
{
	for (int i(0) ; i < n ; ++i)
	{
		std::scanf("%d%d%d",&boxes[i][0],&boxes[i][1],&boxes[i][2]);
		sorted[i] = i;
	}
}
/*
inline int cmp (int a, int b)
{
	for (int i(0) ; i < DIMENSIONS ; ++i)
		if (boxes[a][i] < boxes[b][i])
			return -1;
		else if (boxes[a][i] > boxes[n][i])
			return 1;
	return 0;
}*/

bool cmp (int a, int b)
{
	for (int i(0) ; i < DIMENSIONS ; ++i)
		if (boxes[a][i] < boxes[b][i])
			return true;
		else if (boxes[a][i] > boxes[n][i])
			return false;
	return false;
}

inline bool fit (int a, int b)
{
	return (boxes[a][0] < boxes[b][0] && boxes[a][1] < boxes[b][1] && boxes[a][2] < boxes[b][2]);
}

void quicksort (int left, int right)
{
	int pivot(sorted[left + (rand() % (right - left + 1))]), i(left), j(right), temp;
	while (i <= j)
	{
		while (cmp(sorted[i],pivot) < 0)
			++i;
		while (cmp(sorted[j],pivot) > 0)
			--j;
		if (i <= j)
		{
			temp = sorted[i];
			sorted[i] = sorted[j];
			sorted[j] = temp;
			++i;
			--j;
		}
	}
	if (left < j)
		quicksort(left,j);
	if (right > i)
		quicksort(i,right);
}

inline int maximal_substring (int begin, int end)
{
	*substring = 0;
	int j(0), left, right, middle;
	for (int i(1) ; i < end ; ++i)
	{
		if (fit(sorted[substring[j]],sorted[i]))
		{
			++j;
			substring[j] = i;
		}
		else
		{
			left = 0;
			right = j;
			middle = (left + right) >> 1;
			while (left < right)
			{
				if (cmp(sorted[i],sorted[substring[middle]]) > 0)
					left = middle + 1;
				else
					right = middle;
				middle = (left + right) >> 1;
			}
			if (fit(sorted[i],sorted[substring[middle]]))
				substring[middle] = i;
		}
	}
	return j + 1;
}

int main (void)
{
	std::srand(std::time(0));
	std::freopen("cutii.in","r",stdin);
	std::freopen("cutii.out","w",stdout);
	std::scanf("%d%d",&n,&t);
	do
	{
		read();
		//quicksort(0,n - 1);
		std::sort(sorted,sorted + n);
		std::printf("%d\n",maximal_substring(0,n));
		--t;
	}
	while (t);
	std::fclose(stdin);
	std::fclose(stdout);
	return 0;
}