HDU 1051 Wooden Sticks

题目地址:点击打开链接

思路:贪心+动归

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct wood
{
	int head;
	int tail;
	int flag;
}a[5010];
int cmp(struct wood x,struct wood y)
{
	if(x.head != y.head)
		return x.head < y.head;
	else
		return x.tail < y.tail;
}
int main()
{
	int t,n,i,j,ans,end;
	scanf("%d",&t);
	while(t--)
	{
		ans = 0;
		scanf("%d",&n);
		for(i=0; i<n; i++)
		{
			scanf("%d%d",&a[i].head,&a[i].tail);
			a[i].flag = 1;
		}
		sort(a,a+n,cmp);
		for(i=0; i<n; i++)
		{
			if(a[i].flag)
			{
				ans++;
				a[i].flag = 0;
				end = a[i].tail;
				for(j=i+1; j<n; j++)
				{
					if(a[j].flag  && a[j].tail >= end)
					{
						a[j].flag = 0;
						end = a[j].tail;
					}
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(HDU 1051 Wooden Sticks)