hdu 1050 Moving Tables 解题报告

时间:2023-03-10 04:45:04
hdu 1050 Moving Tables 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050

这道题目隔了很久才做出来的。一开始把判断走廊有重叠的算法都想错了。以为重叠只要满足,下一次moving的起始room小于或等于上一次moving的结束room则证明有重复。这样只能保证局部不能同时进行moving,但是根本得不出其他moving哪些是可以同时进行搬动的。

正确的思路是,统计最大的重叠数,再乘以10即可。具体做法:把每个房间之间的走廊作为一个统计单位,当所有的办公桌都搬运完成之后,看看这段走廊到底需要占用多少次,然后统计所有的走廊被占用的最大值max,这个值就是要单独安排的搬运次数,乘以10就是总的搬运时间。

该算法属于贪心算法,因为它尽可能使搬运办公桌同时进行,以便使单独安排的搬运次数最少。

 #include <iostream>
#include <algorithm>
using namespace std; int main()
{
int i, n, cas, from, to, max, table[];
while (scanf("%d", &cas) != EOF)
{
while (cas--)
{
memset(table, , sizeof(table));
scanf("%d", &n);
while (n--)
{
scanf("%d%d", &from, &to);
if (to < from) // 保证from < to
swap(from, to);
from = (from - ) / ; // 将房间号折算成走廊号
to = (to - ) / ;
for (i = from; i <= to; i++) // 占用走廊情况
{
table[i]++; // 保存占用走廊的情况
}
}
max = ;
/* for (i = 0; i <= 200; i++)
{
printf("table[%d] = %d\n", i, table[i]);\
} */
for (i = ; i <= ; i++)
{
if (max < table[i])
max = table[i];
}
printf("%d\n", max * ); // 总的搬运时间
}
}
return ;
}