HDU 1051 与1257类似 二维排序,然后贪心

#include <cstdio>  
#include <string>  
#include <algorithm>  
#include <iostream>
using namespace std;  
 
typedef struct
{
	int x, y;
} node;

bool cmp(node n1, node n2)
{
	if(n1.x != n2.x)
		return (n1.x < n2.x);
	else
		return (n1.y < n2.y);
}
node a[5010];
int len = 0;

int min(int *b)
{
	int min = 1000000;
	for(int i = 0; i < len; i++)
	{
		if(min > b[i])
			min = b[i];
	}
	return min;
}

int maxIndex(int *b, int a)
{
	int max = 0;
	int index = 0;
	for(int i = len -1; i >= 0; i--)
	{
		if(b[i] <= a && b[i] >= max)
		{
			max = b[i];
			index = i;
		}
	}
	return index;
}
int main()  
{ 
	int t;
	cin >> t;
	while(t--)
	{
		len = 0;
		int n;
		cin >> n;
		int *b = new int[n+1];
		for(int i = 0; i < n; i++)
		{
				scanf("%d%d", &a[i].x, &a[i].y);
		}
		sort(a, a+n, cmp);
		for(int i = 0; i < n; i++)
		{
			if(a[i].y < min(b))
			{
				b[len++] = a[i].y;
			}
			else
			{
				b[maxIndex(b, a[i].y)] = a[i].y;
			}
		}
		cout << len << endl;
		delete [] b;
	}
    return 0;  
} 


1050不能用这个方法,因为其起始点分为奇偶数

你可能感兴趣的:(HDU 1051 与1257类似 二维排序,然后贪心)