sicily 1193. Up the Stairs
2015-10-30 20:55
507 查看
1193. Up the Stairs
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
John is moving to the penthouse of a tall sky-scraper. He packed all his stuff in boxes and drove them to
the entrance of the building on the ground floor. Unfortunately the elevator is out of order, so the boxes have to be moved up the stairs.
Luckily John has a lot of friends that want to help carrying his boxes up. They all walk the stairway at the same speed of 1 floor per minute, regardless of whether they carry a box or not. The stairway however is so narrow that two persons can't pass each
other on it. Therefore they deciced to do the following: someone with a box in his hands is always moving up and someone empty-handed is always moving down. When two persons meet each other somewhere on the stairway, the lower one (with a box) hands it over
to the higher one (without a box). (And then the lower one walks down again and the higher one walks up.) The box exchange is instantaneous. When someone is back on the ground floor, he picks up a box and starts walking up. When someone is at the penthouse,
he drops the box and walks down again.
After a while the persons are scattered across the stairway, some of them with boxes in their hands and some without. There are still a number of boxes on the ground floor and John is wondering how much more time it will take before all the boxes are up. Help
him to find out!
Input
One line with a positive number: the number of test cases. Then for each test case:One line with three numbers N, F, B with 1 ≤ N,F ≤ 1000 and 1 ≤ B ≤ 1000000: the number of persons, the number of floors (0=ground floor, F=penthouse) and the number of boxes that are still on the ground floor.
N lines with two numbers fi and bi with 0 ≤ fi ≤ F and bi = 0 or bi = 1: the floors where the persons are initially and whether or not they have a box in their hands (1=box, 0=no box).
Output
One line with the amount of time (in minutes) it will take to get all the remaining boxes to the penthouse.Sample Input
2 3 10 5 0 0 0 0 0 0 2 5 1 2 1 3 0
Sample Output
30 8
题目分析
搬箱子上楼梯,题目大意是
楼梯上站着n个人,楼下有m件货品,
拿着东西的人只能上楼,没拿东西的人只能下楼,
如果相遇了两人则传递物品,交换身份
问最终耗时
初看题目,直接模拟的话要考虑判断很多情况,互换身份很麻烦
思考后发现扩宽楼道,即让每个人自己走自己的路,其实总的用时是一样的
直接模拟,超时了
别人的思路是直接确定哪个人会拿最后一个,然后确定用时
想想确实有道理
根据每个人现在的位置和状态,不难确定他会去拿第几个箱子
(有箱子的先向上再向下,没箱子的直接向下)
之后每轮拿箱子都是这个次序
交付第一轮箱子只要上一次楼梯即可
之后的每轮交付都需要先下再上
只要确定了是哪个人在第几轮交付即可
#include <cstdio>
#include <iostream>
#include <algorithm>
struct Person {
int pos;
int withgoods;
int steps;
};
bool com(Person a, Person b) {
return a.steps < b.steps;
}
int main()
{
int test;
scanf("%d", &test);
while (test--) {
int people, floors, goods;
scanf("%d%d%d", &people, &floors, &goods);
Person per[people];
for (int i = 0; i < people; ++i) {
scanf("%d%d", &per[i].pos, &per[i].withgoods);
if (per[i].withgoods) {
per[i].steps = floors - per[i].pos + floors;
} else {
per[i].steps = per[i].pos;
}
}
std::sort(per, per+people, com);
int ans;
int count = goods / people;
int index = (goods-1) % people;
if (index == people-1) {
ans = per[index].steps + (count*2-1) * floors;
} else {
ans = per[index].steps + (count*2+1) * floors;
}
printf("%d\n", ans);
}
}
相关文章推荐
- AIR文件操作:使用文件对象操作文件和目录 .
- multithreading--synchronized对象锁以及对象的notify(),notifyAll(),wait()等的用法
- sicily 1093. Air Express
- H - Relief grain
- 线程的sleep()和wait() 区别
- 给hmailserver添加SSL支持
- SendMail
- HDU 4300 Clairewd’s message (kmp | exkmp)
- 通过CONSTRAINTS查看表之间的主外键关系
- STM_HAL: assert_param 与 assert_failed函数
- BZOJ 4320 ShangHai2006 Homework 并查集
- OS X EI Captain安装Cocoapods问题
- Aizu-0558 Cheese
- bzoj-1535 Sza-Template
- Aizu-0033 Ball
- ORA-00054 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效"的快速解决方法
- 如何签名apk,并让baidu地图正常显示
- Fail to connect to camera service的几种原因和解决方法
- 解决Linux报错:httpd: Could not reliably determine the server's fully qualified domain name, using localh
- raise语句