HDU 1051 Wooden Sticks 貪心

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

題目大意:spa

給定一些木棒的長和重,安裝第一根木棒時間爲1分鐘,而後若是安裝的上一支木棒的長和重均不超過下一支木棒的長和重,那麼不須要安裝時間,不然要1分鐘。code

求最短的安裝時間。get

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

按照(1,4), (3,5), (4,9), (2,1), (5,2) 只須要2分鐘。(第一根1分鐘,以後(4,9)轉到(2,1)還須要1分鐘)it



思路:io

我是去練DP的啊啊啊啊,一看就知道是貪心。想半天DP不出來也沒看到誰用DP的。QAQclass

廢話很少說,排個選取的時候向後找,直到找不到沒安裝的木棒爲止。sed

複雜度O(n^2)sort



#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\n", ans);
	}
	return 0;
}
相關文章
相關標籤/搜索