`
phinecos
  • 浏览: 342179 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ZOJ1029 Moving Tables

 
阅读更多
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->#include<iostream>
#include
<vector>
#include
<algorithm>
usingnamespacestd;

constintTIMEPERMOVE=10;//每次分钟
constintMAXSIZE=200;
structMove
{
intsrcRoom;//
intdesRoom;//目标
}moves[MAXSIZE];

boollessThan(constMove&m1,constMove&m2)
{
returnm1.srcRoom<m2.srcRoom;//按照srcRoom从小到大排序
}

intmain(void)
{
intCases,i,j,k,n,s,t;
cin
>>Cases;
for(i=1;i<=Cases;++i)
{
vector
<Move>moveVect;
cin
>>n;
for(j=0;j<n;++j)
{
cin
>>s;
cin
>>t;
if(s>t)
swap(s,t);
moves[j].srcRoom
=(s+1)/2;
moves[j].desRoom
=(t+1)/2;
moveVect.push_back(moves[j]);
}
//排序
sort(moveVect.begin(),moveVect.end(),lessThan);
intmax=0;
for(j=0;j<n;++j)
{
intcount=1,from=moveVect[j].srcRoom,to=moveVect[j].desRoom;
for(k=0;k<n;++k)
{
if(j==k)continue;
if(moveVect[k].srcRoom<=to&&moveVect[k].desRoom>=from)
{
if(from<moveVect[k].srcRoom)
from
=moveVect[k].srcRoom;
if(to>moveVect[k].desRoom)
to
=moveVect[k].desRoom;
count
++;
}
}
if(count>max)
max
=count;
}
cout
<<TIMEPERMOVE*max<<endl;
}
return0;
}

还有人给出了不使用贪心的算法,贪心是将每次能同时搬运的桌子都搬运,求总共需要次数。能否同时搬运桌子,取决于搬运使用的走廊是否被占用。因此,实际上我们只需要求出,走廊最多被占用多少次,就可以得出最多要花多少时间,实在是高!

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

#defineMAXN201
intmap[MAXN];

voidsolve()
{
inti,n,start,end,m;

for(i=0;i<MAXN;i++)//初始化
map[i]=0;

cin
>>n;
while(n--)
{
cin
>>start;
cin
>>end;
if(start>end)
{
inttemp=start;
start
=end;
end
=temp;
}
for(i=(start+1)/2;i<=(end+1)/2;i++)
map[i]
+=1;
}

m
=map[1];
for(i=2;i<MAXN;i++)
{
if(map[i]>m)
m
=map[i];
}
cout
<<m*10<<endl;
}

intmain()
{
intt;
cin
>>t;
while(t--)
solve();
return0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics