标签:贪心
C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?
3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1
2 1 3
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct stick{
double l,w;
int flag;
}s[10010];
bool cmp(stick a,stick b)
{
if(a.l!=b.l)
return a.l<=b.l;
else
return a.w<=b.w;
}
int main()
{
int t,i,j,n;
scanf("%d",&t);
while(t--)
{
// memset(s,0,sizeof(s));
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%lf%lf",&s[i].l,&s[i].w);
s[i].flag=0;
}
sort(s,s+n,cmp);
int count=0;
for(i=0;i<n;++i)
{
double t=s[i].w;
if(s[i].flag)//judge the signal for the time limits
{
continue;
}
for(j=i+1;j<n;++j)
{
if(!s[j].flag&&t<=s[j].w)
{
s[j].flag=1;//make a signal for the next judge
t=s[j].w;//switch the variable
}
}
count++;//the numbles that circal goes
}
printf("%d\n",count);
}
return 0;
} 标签:贪心
原文地址:http://blog.csdn.net/ice_alone/article/details/41443765