poj2772

题意:一些人把一些箱子搬到楼上,这些人从各自的位置以相同的速度移动,有的向上,有的向下,向上的有箱子,向下的没箱子。到顶后放下箱子同速返回。若两人相遇,则向上的人把箱子给向下的人,同时两人同时转身向反方向走。问把箱子全搬上去要用多久。

分析:我们可以忽略题中的人,认为只是一些箱子在移动,这些箱子的移动规律很明显,就是在不停地上升。我们还可以认为,两个人相遇并没有交换箱子,而是擦肩而过,这样对箱子和人都没有任何影响。因为a上b下,a,b相遇,就变成了a替b向上,b替a向下。所以本题就变成了所有的人轮流来一楼取箱子,取完了就向上走,走到楼顶,再走下来,再取。这样只要算出整体轮回了多少次,再加上多余的几个箱子的搬运时间即可。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
usingnamespace std;

#define maxn 1005

int n, floor, b;
int f[maxn];

void input()
{
scanf(
"%d%d%d", &n, &floor, &b);
for (int i =0; i < n; i++)
{
int a, b;
scanf(
"%d%d", &a, &b);
if (b)
f[i]
= floor *2- a;
else
f[i]
= a;
}
}

void work()
{
int ans = floor;
ans
+= b / n *2* floor;
b
%= n;
if (b ==0)
ans
-=2* floor - f[n -1];
else
ans
+= f[b -1];
printf(
"%d\n", ans);

}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
input();
sort(f, f
+ n);
work();
}
return0;
}
原文地址:https://www.cnblogs.com/rainydays/p/2116608.html