题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1051
题目大意:
一堆木头,给出长度和重量,(L,m),给出两个条件
1.一根木头的setup time是1分钟。(英语太渣,不知道是毛意思,不过不影响做题有木有)
2.假设处理完一根木头,下一根木头若是满足,长度和重量大于或等于它,则不需要时间。
问处理完所有木头需要多少时间。
思路:
感觉可以用贪心,就试试了。
排序,长度短的排前面,长度相等的话,重量小的排前面。
代码:
#include<cstdio>
#include<cstring>
#include<stdlib.h>
struct In
{
int length;
int weight;
}a[5001];
int flag[5001];
int cmp(const void *a,const void *b)
{
struct In*c=(In*)a;
struct In*d=(In*)b;
if(c->length!=d->length) return c->length-d->length;
else return c->weight-d->weight;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].length,&a[i].weight);
}
qsort(a,n,sizeof(a[0]),cmp);
memset(flag,0,sizeof(flag));
int before=a[0].weight;
int ans=1;
while(1)
{
for(i=1;i<n;i++)
{
if(flag[i]==0&&a[i].weight>=before)
{
before=a[i].weight;
flag[i]=1;
}
}
for(i=1;i<n;i++)
{
if(flag[i]==0)
{
before=a[i].weight;
flag[i]=1;
ans++;
break;
}
}
if(i==n) break;
}
printf("%d\n",ans);
}
return 0;
}