Cod sursa(job #90411)

Utilizator mugurelionutMugurel-Ionut Andreica mugurelionut Data 9 octombrie 2007 13:26:55
Problema Hvrays Scor Ascuns
Compilator cpp Status done
Runda Marime 1.22 kb
#include <stdio.h>

#include <algorithm>
#include <vector>

using namespace std;

#define HORIZ 0
#define VERT 1
#define INF 666666666
#define NMAX 100100

int i, j, k, H, Hnew, V, nv, ncmax;
int xh[NMAX], yh[NMAX], xv[NMAX], yv[NMAX], nc[NMAX];

int main()
{
	int te, T;

	freopen("hvrays.in", "r", stdin);
	freopen("hvrays.out", "w", stdout);

	scanf("%d", &T);

	for (te = 1; te <= T; te++)
	{
		scanf("%d %d", &H, &V);

		for (i = 1; i <= H; i++)
			scanf("%d %d", &xh[i], &yh[i]);

		for (i = 1; i <= V; i++)
			scanf("%d %d", &xv[i], &yv[i]);

		nv = 0;

		while (H > 0 && V > 0)
		{
			ncmax = 0;

			for (i = 1; i <= V; i++)
			{
				nc[i] = 0;

				for (j = 1; j <= H; j++)
					if (xh[j] <= xv[i] && yh[j] <= yv[i])
						nc[i]++;

				if (nc[i] > ncmax)
					ncmax = nc[i],
					k = i;
			}

			if (!ncmax)
				break;

			nv++;

			Hnew = 0;

			for (j = 1; j <= H; j++)
				if (!(xh[j] <= xv[k] && yh[j] <= yv[k]))
				{
					Hnew++;
					xh[Hnew] = xh[j];
					yh[Hnew] = yh[j];
				}

			H = Hnew;

			for (i = k + 1; i <= V; i++)
			{
				xv[i - 1] = xv[i];
				yv[i - 1] = yv[i];
			}

			V--;
		}

		if (H > 0)
			nv = -1;

		printf("%d\n", nv);
		fprintf(stderr, "-> Sol=%d [Hleft=%d; Vleft=%d]\n", nv, H, V);
	}

	return 0;
}