码迷,mamicode.com
首页 > 其他好文 > 详细

greedy1065

时间:2014-09-15 19:21:49      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   io   os   for   sp   on   c   amp   new   

有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列。问至少要分为多少个序列。

自己写的,第一次就通过了,牛逼啊~~~~~~~~~~

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

 

typedef struct ss

{

int l,w;

bool used;

}stick;

 

stick s[5001];

 

int cmpmax1(stick a,stick b)

{

if(a.l==b.l)

return a.w<b.w;

 

return a.l<b.l;

}

 

int cmpmax2(stick a,stick b)

{

if(a.w==b.w)

return a.l<b.l;

 

return a.w<b.w;

}

 

 

int main()

{

int t,i,num,totalnum,j;

 

 

scanf("%d",&t);

while(t--)

{

scanf("%d",&num);

memset(s,0,sizeof(s));

for(i=1;i<=num;i++)

scanf("%d%d",&s[i].l,&s[i].w);

 

sort(s+1,s+1+num,cmpmax1);

 

int templ=s[1].l,tempw=s[1].w;

totalnum=0;

int cost=0;

 

 

while(totalnum!=num)

{

cost++;

for(i=1;i<=num;i++)

if(s[i].used==0)

{

templ=s[i].l;

tempw=s[i].w;

s[i].used=1;

totalnum++;

break;

}

 

for(j=i+1;j<=num;j++)

{

if(s[j].used==0 && s[j].l>=templ && s[j].w>=tempw)//实际上这里只需要比较w即可,因为l已经被事先排序。

{

s[j].used=1;

templ=s[j].l;

tempw=s[j].w;

totalnum++;

}

}

}

printf("%d\n",cost);

}

return 1;

}

greedy1065

标签:style   io   os   for   sp   on   c   amp   new   

原文地址:http://www.cnblogs.com/notlate/p/3973446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!