HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)
Description
After months of climbing, Mike feels that he can’t stand it any more. He wants to sue the building owner. In order to let the judge understand how terrible the situation is, he decides to write a program to simulate the running of the elevator in a day. You’d better let him copy one from you .
At first, the elevator is at the status of “idle”. If the three conditions below are all satisfied at the same time, we say the elevator is at “idle” status: 1) The elevator is stopped. 2) Nobody outside is waiting for the elevator. 3) There is nobody in the elevator or all people in the elevator are just on their destination floor.
There are an up button and a down button at the elevator gate on every floor except that only up button on the first floor, and only down button on the 50th floor. When someone wants to take the elevator, he pushes a button according to the direction he wants to go, and then wait. If the elevator is not moving towards his destination floor, he will not get in even the elevator comes and opens its door. When someone pushes a button, we say that he send a request to the elevator.
When the elevator is idle and then some requests are sent to it, it will move towards the direction from which the first request is sent. If more than one request is sent at the same time, the requests sent form the same floor where the elevator stays have higher priority. In other cases, requests which will make the elevator go up, have higher priority than the same time requests which will make the elevator go down.
Once the elevator starts moving, it keeps its moving direction until the three conditions below are all satisfied at the same time: 1) All the people in the elevator have reached their destination floor. 2) There is nobody waiting for the elevator at the elevator’s moving direction. 3) Nobody on the floor where the elevator stays wants to go towards the elevator’s moving direction. When the three conditions above are all satisfied at the same time, if there are requests from the direction opposite to the elevator’s last moving direction, the elevator will turn around and start moving; and if there are no requests at that time, the elevator will stay there and become idle.
When the elevator reaches a certain floor, it will stop and open its door when one of the two conditions below is satisfied: 1) Someone inside the elevator wants to get off on that floor. 2) Someone on that floor wants to go towards the elevator’s moving direction.
It takes one second for the elevator to move from one floor to another. It takes one second for the elevator to open the door or close the door. It takes one second for people outside the elevator to get in, no mater how many people. It takes one second for people inside the elevator go get out, no mater how many people.
The elevator can’t stop between two floors.
Input
For each test case:
The first line contains two integers: i and n. The elevator is on the i-th floor at first, and n is the total number of requests. ( 1 <= i <= 50, 1<=n<=100) Then n lines follow. Each line contains three integers: t, s and d. It means that at the time of t-th second, a person on the s-th floor sends a request, and he wants to go to the d-th floor.
Output
mm:ss The elevator starts to move (up|down) from floor x. mm:ss The elevator stops at floor x. mm:ss The elevator door is opening. mm:ss x people leave the elevator. mm:ss x people enter the elevator. mm:ss The elevator door is closing.
"mm:ss" means time, "mm" for minute, "ss" for second . Please append a blank line to the end of the output of each test case. It is guaranteed that the elevator will finish all requests within 3600 seconds。
题目大意:模拟一台电梯的运作,细节不多说了。要注意的是:如果电梯在闲置状态,电梯所在层同时有人上有人下,就优先上,这个题目说得不太好(还是我英语问题呢……);电梯会一直走同一个方向直到没有人须要电梯往那个方向走了;至于POJ的DISCUSS里面有人说有进出是同一层的情况,我测试了一下(if(from == to) tle();),是没有这种情况的……
思路:丧心病狂模拟题,打错一个字母就没有然后了(还好我是复制的)。每次时间+1都判断一下有没有新的人来坐电梯,慢慢搞总会AC的……
PS:本人第一条200+行的题(大概是)。做了几个小时,现场肯定不能做了,可能我做法有点挫就不写做法误导大家了(捂脸)。
代码(HDU 0MS/POJ 16MS):
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXT = ; struct Node {
int t, from, to;
void read() {
scanf("%d%d%d", &t, &from, &to);
}
bool operator < (const Node &rhs) const {
return t < rhs.t;
}
}; Node a[MAXN];
int leave[MAXN], sum_leave;//要在第几层离开
int into[MAXN][], sum_into;//要在第几层进来,0:down,1:up
bool have_in[MAXN];//已进入电梯
int T, n, rec_t, rec_a, rec_f, step, state; void inc_time() {
++rec_t;
while(rec_a < n && a[rec_a].t <= rec_t) {//更新请求
if(a[rec_a].from > a[rec_a].to) {
++into[a[rec_a].from][];
}
else {
++into[a[rec_a].from][];
}
++sum_into;
++rec_a;
}
} void solve() {
sort(a, a + n);
step = ;
sum_leave = sum_into = ;
rec_a = ;
rec_t = -; inc_time();
memset(have_in, , sizeof(have_in));
while(rec_a < n || sum_into || sum_leave) {
switch(step) {
case : { //空闲
if(sum_into == ) {
inc_time();
continue;
}
if(into[rec_f][]) {
step = ;//开门
state = ;//向上
continue;
}
if(into[rec_f][]) {
step = ;//开门
state = ;//向下
continue;
}
for(int i = rec_f + ; i <= ; ++i)
if(into[i][] || into[i][]) {//上面有请求
step = ;//向上
state = ;
break;
}
if(step == ) {
printf("%02d:%02d The elevator starts to move up from floor %d.\n", rec_t / , rec_t %, rec_f);
continue;
}
for(int i = ; i < rec_f; ++i)
if(into[i][] || into[i][]) {//下面有请求
step = ;//向下
state = ;
break;
}
if(step == ) {
printf("%02d:%02d The elevator starts to move down from floor %d.\n", rec_t / , rec_t %, rec_f);
continue;
}
break;
}
case : {//开门
printf("%02d:%02d The elevator door is opening.\n", rec_t / , rec_t %);
inc_time();
step = ;//离开
break;
}
case : {//关门
printf("%02d:%02d The elevator door is closing.\n", rec_t / , rec_t %);
inc_time();
if(into[rec_f][state]) {//有人要进来
step = ;
continue;
}
step = ;
break;
}
case : {//向上
++rec_f;
inc_time();
if(into[rec_f][] || leave[rec_f]) {
printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / , rec_t %, rec_f);
step = ;
continue;
}
if(sum_leave) continue;
bool flag = true;
for(int i = rec_f + ; i <= ; ++i) {
if(into[i][] || into[i][]) {
flag = false;
break;
}
}
if(flag) {
printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / , rec_t %, rec_f);
state = , step = ;
}
break;
}
case : {//向下
--rec_f;
inc_time();
if(into[rec_f][] || leave[rec_f]) {
printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / , rec_t %, rec_f);
step = ;
continue;
}
if(sum_leave) continue;
bool flag = true;
for(int i = ; i < rec_f; ++i) {
if(into[i][] || into[i][]) {
flag = false;
break;
}
}
if(flag) {
printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / , rec_t %, rec_f);
state = , step = ;
}
break;
}
case : {//进入
if(into[rec_f][state]) {
printf("%02d:%02d %d people enter the elevator.\n", rec_t / , rec_t %, into[rec_f][state]);
sum_into -= into[rec_f][state];
into[rec_f][state] = ;
for(int i = ; i < rec_a; ++i)
if(a[i].from == rec_f && state == (a[i].from < a[i].to) && !have_in[i]) {
have_in[i] = true;
++leave[a[i].to];
++sum_leave;
//in_ele[i] = true;
}
inc_time();
}
if(!into[rec_f][state]) step = ;//有人要进来就不关门
break;
}
case : {//离开
if(leave[rec_f]) {
printf("%02d:%02d %d people leave the elevator.\n", rec_t / , rec_t %, leave[rec_f]);
sum_leave -= leave[rec_f];
leave[rec_f] = ;
inc_time();
}
if(sum_leave == && state == ) {
bool flag = true;
for(int i = ; i < rec_a; ++i) {
if(!have_in[i] && a[i].from == rec_f && a[i].to > rec_f) {
flag = false;
break;
}
if(!have_in[i] && a[i].from > rec_f) {
flag = false;
break;
}
}
if(flag) state = ;
}
else if(sum_leave == && state == ) {
bool flag = true;
for(int i = ; i < rec_a; ++i) {
if(!have_in[i] && a[i].from == rec_f && a[i].to < rec_f) {
flag = false;
break;
}
if(!have_in[i] && a[i].from < rec_f) {
flag = false;
break;
}
}
if(flag) state = ;
}
step = ;
break;
}
case : {//判断关门后动作
if(sum_into == && sum_leave == ) {//没人要进来没人在电梯里
step = ;
continue;
}
if(sum_leave == ) {//本层没人上,电梯没人,有请求
if(state == ) {//是否继续向上
int i;
for(i = rec_f + ; i <= ; ++i) {
if(into[i][] || into[i][]) break;
}
if(i > ) state = ;
}
else {//是否继续向下
int i;
for(i = ; i < rec_f; ++i) {
if(into[i][] || into[i][]) break;
}
if(i == rec_f) state = ;
}
}
if(state) printf("%02d:%02d The elevator starts to move up from floor %d.\n", rec_t / , rec_t %, rec_f);
else printf("%02d:%02d The elevator starts to move down from floor %d.\n", rec_t / , rec_t %, rec_f);
if(state) step = ;
else step = ;
break;
}
}
}
printf("%02d:%02d The elevator door is closing.\n", rec_t / , rec_t %);
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &rec_f, &n);
for(int i = ; i < n; ++i) a[i].read();
printf("Case %d:\n", t);
solve();
puts("");
}
}
HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)的更多相关文章
- HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)
Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...
- HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)
Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...
- HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)
Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...
- HDU 2487 Ugly Windows(暴力)(2008 Asia Regional Beijing)
Description Sheryl works for a software company in the country of Brada. Her job is to develop a Win ...
- hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)
Mart Master II Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...
- hdu 2473 Junk-Mail Filter(并查集_虚节点)2008 Asia Regional Hangzhou
感觉有些难的题,刚开始就想到了设立虚节点,但是实现总是出错,因为每次设立了虚节点之后,无法将原节点和虚节点分开,导致虚节点根本无意义. 以上纯属废话,可以忽略…… 题意—— 给定n个点(0, 1, 2 ...
- Hdu OJ 5884-Sort (2016 ACM/ICPC Asia Regional Qingdao Online)(二分+优化哈夫曼)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题目大意:有n个有序的序列,对于第i个序列有ai个元素. 现在有一个程序每次能够归并k个序列, ...
随机推荐
- 840. Magic Squares In Grid (5月27日)
开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...
- 安装psutil时提示缺少python.h头文件(作记录)
通过pip或者源码安装psutil,都会提示缺少python.h头文件,错误提示如下: ... psutil/_psutil_common.c:9:20: fatal error: Python.h: ...
- PHP7.1中使用openssl替换mcrypt
PHP7.1中使用openssl替换mcrypt 在php开发中,使用mcrypt相关函数可以很方便地进行AES加.解密操作,但是PHP7.1中废弃了mcrypt扩展,所以必需寻找另一种实现.在迁移手 ...
- 『Python基础-11』集合 (set)
# 『Python基础-11』集合 (set) 目录: 集合的基本知识 集合的创建 访问集合里的值 向集合set增加元素 移除集合中的元素 集合set的运算 1. 集合的基本知识 集合(set)是一个 ...
- ruby 正则表达式Regexp
ruby正则表达式在线编辑器:rubular 一般规则: /a/匹配字符a. /\?/匹配特殊字符?.特殊字符包括^, $, ? , ., /, \, [, ], {, }, (, ), + ...
- java语言描述 用递归打印字符串
public class Test{ static private int n; public static void main(String[] args) { Test.n=76234; if(n ...
- 青岛Uber优步司机奖励政策(1月11日~1月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 长沙Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- mysql主从集群搭建;(集群复制数据)
1.搭建mysql 5.7环境chown mysql:mysql -R /data/groupadd mysqluseradd -g mysql mysql yum install numactlrp ...
- Kafka在高并发的情况下,如何避免消息丢失和消息重复?kafka消费怎么保证数据消费一次?数据的一致性和统一性?数据的完整性?
1.kafka在高并发的情况下,如何避免消息丢失和消息重复? 消息丢失解决方案: 首先对kafka进行限速, 其次启用重试机制,重试间隔时间设置长一些,最后Kafka设置acks=all,即需要相应的 ...