ZOJ1025 Wooden Sticks

典型的贪心算法,和前面1029一样也是工作时间调度问题。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> #include < iostream >
#include
< vector >
#include
< algorithm >
using namespace std;

const int MAXSIZE = 5000 ;
struct stick
{
// 木条
int length; // 长度
int width; // 宽度
bool isVisited;
}Sticks[MAXSIZE];

bool lessThan( const stick & s1, const stick & s2)
{
// 按木条长度从大到小排序
return s1.length < s2.length;
}
int main( void )
{
int cases,i,j,k,n;
cin
>> cases;
for (i = 1 ;i <= cases; ++ i)
{
cin
>> n;
vector
< stick > stickVect;
for (j = 0 ;j < n; ++ j)
{
cin
>> Sticks[j].length >> Sticks[j].width;
Sticks[j].isVisited
= false ;
stickVect.push_back(Sticks[j]);
}
sort(stickVect.begin(),stickVect.end(),lessThan);
// 排序
// 贪心法
int length,width,time = 0 ,count = 0 ;
for (j = 0 ;j < n; ++ j)
{
if (stickVect[j].isVisited == false )
{
// 还未访问,作为新一轮的排头兵
time ++ ; // 新一轮了,时间加
length = stickVect[j].length;
width
= stickVect[j].width;
stickVect[j].isVisited
= true ;
for (k = 0 ;k < n; ++ k)
{
if (stickVect[k].isVisited == false )
{
if (stickVect[k].length >= length && stickVect[k].width >= width)
{
// 此木条可以和排头兵一起顺利通过
stickVect[k].isVisited = true ;
length
= stickVect[k].length;
width
= stickVect[k].width;
}
}
}
}
}
cout
<< time << endl;
}
return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值