#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int TIMEPERMOVE = 10;//每次分钟
const int MAXSIZE = 200;
struct Move
{
int srcRoom;//源
int desRoom;//目标
}moves[MAXSIZE];
bool lessThan(const Move& m1,const Move& m2)
{
return m1.srcRoom<m2.srcRoom; //按照srcRoom从小到大排序
}
int main(void)
{
int Cases,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);
int max = 0;
for (j=0; j<n; ++j)
{
int count = 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;
}
return 0;
}
复制代码
还有人给出了不使用贪心的算法,贪心是将每次能同时搬运的桌子都搬运,求总共需要次数。能否同时搬运桌子,取决于搬运使用的走廊是否被占用。因此,实际上我们只需要求出,走廊最多被占用多少次,就可以得出最多要花多少时间,实在是高!
复制代码
#include <iostream>
using namespace std;
#define MAXN 201
int map[MAXN];
void solve()
{
int i,n,start,end,m;
for(i=0;i<MAXN;i++)//初始化
map[i] = 0;
cin >> n;
while(n--)
{
cin >> start;
cin >> end;
if(start > end)
{
int temp = 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;
}
int main()
{
int t;
cin >> t;
while(t--)
solve();
return 0;
}
复制代码
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/10/28/1321489.html,如需转载请自行联系原作者