//贪心算法
#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 5005
using namespace std;
struct node
{
int len;
int wht;
int flag;
}stick[MAXN];
int cmp(struct node x,struct node y)
{
if(x.len!=y.len)
return x.len<y.len;
else
return x.wht<y.wht;
}
int main()
{
int test,n,i,j,tpl,tpw,minnum;
scanf("%d",&test);
while(test--)
{
minnum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&stick[i].len,&stick[i].wht);
stick[i].flag=0;
}
sort(stick,stick+n,cmp);
for (i=0;i<n;i++)
{
if(!stick[i].flag)
{
minnum++;
stick[i].flag=1;
tpw=stick[i].wht;
for (j=i+1;j<n;j++)
{
if(stick[j].wht>=tpw && !stick[j].flag)
{
stick[j].flag=1;
tpw=stick[j].wht;
}
}
}
}
printf("%d\n",minnum);
}
return 0;
}hdu1051 Wooden Sticks (贪心)
最新推荐文章于 2024-07-26 09:15:00 发布
本文通过一个具体的实例详细解析了如何使用贪心算法解决特定类型的问题。代码中定义了一个结构体来存储每根木棍的长度和颜色,并通过比较长度和颜色对木棍进行排序,然后采用贪心策略选择最少数量的木棍以满足特定条件。
6万+

被折叠的 条评论
为什么被折叠?



