HDU 1051 Wooden Sticks 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=1051

题目大意:

给定一些木棒的长和重,安装第一根木棒时间为1分钟,然后假设安装的上一支木棒的长和重均不超过下一支木棒的长和重。那么不须要安装时间。否则要1分钟。

求最短的安装时间。

如:(4,9), (5,2), (2,1), (3,5), and (1,4)

依照(1,4), (3,5), (4,9), (2,1), (5,2) 仅仅须要2分钟。(第一根1分钟。之后(4,9)转到(2。1)还须要1分钟)



思路:

我是去练DP的啊啊啊啊,一看就知道是贪心。想半天DP不出来也没看到谁用DP的。QAQ

废话不多说。排个选取的时候向后找,直到找不到没安装的木棒为止。

复杂度O(n^2)



#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 5000;
struct wooden{
	int L, W;
	bool operator <(const wooden& x)const{
		if (L == x.L)
			return W < x.W;
		return L < x.L;
	}
}a[MAXN];
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++)		
			scanf("%d%d", &a[i].L, &a[i].W);
		
		sort(a, a + n);
		bool used[MAXN] = { 0 };
		int ans = 1;
		for (int i = 0; i < n; i++)
		{
			if (used[i])	continue;
			used[i] = true;
			int L=a[i].L, W=a[i].W;
			for (int j = i + 1; j < n; j++)
			{
				if (used[j])	continue;
				if (a[j].W >= W && a[j].L >= L)
				{
					used[j] = true;
					L = a[j].L;
					W = a[j].W;
				}
			}

			bool find = false;
			for (int j = i + 1; j < n; j++)
			{
				if (!used[j])
				{
					find = true;
					break;
				}
			}
			if (!find)
				break;
			ans++;
		}
		printf("%d
", ans);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/wzzkaifa/p/6742987.html