HDOJ 1050 Moving Tables
题目
分类
贪心
题意
搬桌子
有南北各里200间房,和中间走廊 如图
![]()
每次从 房间s 搬到 房间t
走廊每次只能过一张桌子
每次搬运耗时 10min 求 最短时间例如 :
11 搬到 12 ,14 搬到 13 可同时搬运 耗时 10min
1 搬到 4 , 3 搬到 6 不可同时搬运 耗时 20 min
## 题解
很容易 看出贪心策略
保证每 10 min 搬运 走廊都是最大利用 (选出尽可能多的不重叠区间 )
则 耗时最少
## 技巧
不重叠区间
先把区间按左端排好队
每次挑出最左边的区间且与上次挑出区间重叠的区间
重复上述过程即可
## 代码
#include <iostream>
#include <cstring>
#include <algorithm>
#define max 400
using namespace std;
struct path
{
int left;
int right;
bool operator < (const path & b) const
{
if(left < b.left)
return true;
else return false;
}
friend istream & operator>> (istream & in,path & b)
{
int l,r;
in >> l >> r;
if(r < l)
swap(l,r);
b.left = (l + 1)/2;
b.right = (r + 1)/2;
return in;
}
};
path rp[max];
int jdg[max];
int main()
{
int n,m,t,r;
bool f;
cin >> n;
while(n--)
{
cin >> m;
t = 0;
memset(jdg,0,sizeof(jdg));
f = true;
for(int i = 0;i < m;i++)
{
cin >> rp[i];
}
sort(rp,rp+m);
// for(int i = 0;i < m;i++)
// cout << rp[i].left << " " << endl;
while(f)
{
t += 10;
r = -1;
f = false;
for(int i = 0;i < m;i++)
{
if(jdg[i])
continue;
if(r < rp[i].left)
{
jdg[i] = 1;
r = rp[i].right;
f = true;
}
}
}
cout << t - 10 << endl;
}
return 0;
}