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>
usingnamespacestd;

constintMAXSIZE=5000;
structstick
{
//木条
intlength;//长度
intwidth;//宽度
boolisVisited;
}Sticks[MAXSIZE];

boollessThan(conststick&s1,conststick&s2)
{
//按木条长度从大到小排序
returns1.length<s2.length;
}
intmain(void)
{
intcases,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);
//排序
//贪心法
intlength,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;
}
return0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值