//贪心算法
#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 (贪心)
最新推荐文章于 2022-12-03 19:48:18 发布