人生就像一个茶几,上面放着各种杯具,随着时间的增加,杯具也就不断的增多;

废话少说,这次依然是悲剧的题目,我表示我是被英文下倒的。。。尽管我的四级以522过了。。。

题目链接:http://acm.zzuli.edu.cn/showproblem?problem_id=1609

本题所用算法是贪心,具体实现过程,见如下代码:

View Code
#include<stdio.h>    ////题目大意貌似是安装什么木棍,第一个需要1分钟,然后每个如果长度<=上个&&重量<=上个,则不需要额外时间,
#include<string.h>////求安装完所需最小时间
#include<stdlib.h>
struct node
{
int length; ////长度
int weight; ///重量
int flag; ////标记是否用过
}count[5001];

int cmp(const void *a,const void *b)
{
struct node *c=(struct node *)a;
struct node *d=(struct node *)b;
if(c->length!=d->length) ////先按长度排序长短,再按重量排序重轻
return d->length - c->length;
else
return d->weight - c->weight;
}


int main()
{
int T,N,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d%d",&count[i].length,&count[i].weight);
count[i].flag=1;
}
qsort(count,N,sizeof(count[0]),cmp);
int L_length,L_weight,Time;
Time=0;
for(i=0;i<N;i++) /////核心代码,贪心过程
{
if(count[i].flag==1)
{
Time++;
L_length=count[i].length;
L_weight=count[i].weight;
count[i].flag=0;
for(j=0;j<N;j++)
{
if(count[j].length<=L_length&&count[j].weight<=L_weight&&count[j].flag==1) ////仔细理解
{
L_length=count[j].length;
L_weight=count[j].weight;
count[j].flag=0;

}
}
}
}
printf("%d\n",Time);
}
return 0;
}


 

posted on 2012-03-22 20:28  world_ding  阅读(206)  评论(0编辑  收藏  举报