首页 > ACM题库 > HDU-杭电 > hdu 2494 Elevator-模拟-[解题报告]C++
2014
02-09

hdu 2494 Elevator-模拟-[解题报告]C++

Elevator

问题描述 :

Too worrying about the house price bubble, poor Mike sold his house and rent an apartment in a 50-floor building several months ago. This building has only one elevator because it is a so called “rotten tail building”. There are always a lot of people crowding at the gate of the elevator on every floor. Many people have to climb hundreds of steps in order to save time.

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.

输入:

The first line is an integer T indicating the number of test cases. ( T <= 20)

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.

输出:

The first line is an integer T indicating the number of test cases. ( T <= 20)

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.

样例输入:

1
2 9
4 1 3
5 1 3
6 1 3
7 1 3
9 1 3
0 2 1
0 4 1
0 5 6
0 6 1

样例输出:

Case 1:
00:00 The elevator door is opening.
00:01 1 people enter the elevator.
00:02 The elevator door is closing.
00:03 The elevator starts to move down from floor 2.
00:04 The elevator stops at floor 1.
00:04 The elevator door is opening.
00:05 1 people leave the elevator.
00:06 3 people enter the elevator.
00:07 1 people enter the elevator.
00:08 The elevator door is closing.
00:09 The elevator door is opening.
00:10 1 people enter the elevator.
00:11 The elevator door is closing.
00:12 The elevator starts to move up from floor 1.
00:14 The elevator stops at floor 3.
00:14 The elevator door is opening.
00:15 5 people leave the elevator.
00:16 The elevator door is closing.
00:17 The elevator starts to move up from floor 3.
00:19 The elevator stops at floor 5.
00:19 The elevator door is opening.
00:20 1 people enter the elevator.
00:21 The elevator door is closing.
00:22 The elevator starts to move up from floor 5.
00:23 The elevator stops at floor 6.
00:23 The elevator door is opening.
00:24 1 people leave the elevator.
00:25 1 people enter the elevator.
00:26 The elevator door is closing.
00:27 The elevator starts to move down from floor 6.
00:29 The elevator stops at floor 4.
00:29 The elevator door is opening.
00:30 1 people enter the elevator.
00:31 The elevator door is closing.
00:32 The elevator starts to move down from floor 4.
00:35 The elevator stops at floor 1.
00:35 The elevator door is opening.
00:36 2 people leave the elevator.
00:37 The elevator door is closing.
Hint
Hint At time 00:00, there are 4 requests at the same time. The upwards request on the floor where the elevator stays has the highest priority. At time 00:07, because a new person comes, the elevator delays its door closing. At time 00:09, the door is just closed. But a new person comes, so the elevator has to reopen its door.

Description

Too worrying about the house price bubble, poor Mike sold his house and rent an apartment in a 50-floor building several months ago. This building has only one elevator because it is a so called “rotten tail building”. There are always a lot of people crowding at the gate of the elevator on every floor. Many people have to climb hundreds of steps in order to save time. 
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

The first line is an integer T indicating the number of test cases. ( T <= 20) 
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

For each test case, print “Case N:” in a line at first. N is the test case number starting from 1.  Then, print the details of how the elevator runs. You should print information like: 

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 = 110;
 const int MAXT = 3610;
 
 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][2], 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][0];
         }
         else {
             ++into[a[rec_a].from][1];
         }
         ++sum_into;
         ++rec_a;
     }
 }
 
 void solve() {
     sort(a, a + n);
     step = 1;
     sum_leave = sum_into = 0;
     rec_a = 0;
     rec_t = -1; inc_time();
     memset(have_in, 0, sizeof(have_in));
     while(rec_a < n || sum_into || sum_leave) {
         switch(step) {
             case 1: { //空闲
                 if(sum_into == 0) {
                     inc_time();
                     continue;
                 }
                 if(into[rec_f][1]) {
                     step = 2;//开门
                     state = 1;//向上
                     continue;
                 }
                 if(into[rec_f][0]) {
                     step = 2;//开门
                     state = 0;//向下
                     continue;
                 }
                 for(int i = rec_f + 1; i <= 50; ++i)
                     if(into[i][0] || into[i][1]) {//上面有请求
                         step = 4;//向上
                         state = 1;
                         break;
                     }
                 if(step == 4) {
                     printf("%02d:%02d The elevator starts to move up from floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                     continue;
                 }
                 for(int i = 0; i < rec_f; ++i)
                     if(into[i][0] || into[i][1]) {//下面有请求
                         step = 5;//向下
                         state = 0;
                         break;
                     }
                 if(step == 5) {
                     printf("%02d:%02d The elevator starts to move down from floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                     continue;
                 }
                 break;
             }
             case 2: {//开门
                 printf("%02d:%02d The elevator door is opening.\n", rec_t / 60, rec_t %60);
                 inc_time();
                 step = 7;//离开
                 break;
             }
             case 3: {//关门
                 printf("%02d:%02d The elevator door is closing.\n", rec_t / 60, rec_t %60);
                 inc_time();
                 if(into[rec_f][state]) {//有人要进来
                     step = 2;
                     continue;
                 }
                 step = 8;
                 break;
             }
             case 4: {//向上
                 ++rec_f;
                 inc_time();
                 if(into[rec_f][1] || leave[rec_f]) {
                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                     step = 2;
                     continue;
                 }
                 if(sum_leave) continue;
                 bool flag = true;
                 for(int i = rec_f + 1; i <= 50; ++i) {
                     if(into[i][1] || into[i][0]) {
                         flag = false;
                         break;
                     }
                 }
                 if(flag) {
                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                     state = 0, step = 2;
                 }
                 break;
             }
             case 5: {//向下
                 --rec_f;
                 inc_time();
                 if(into[rec_f][0] || leave[rec_f]) {
                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                     step = 2;
                     continue;
                 }
                 if(sum_leave) continue;
                 bool flag = true;
                 for(int i = 0; i < rec_f; ++i) {
                     if(into[i][1] || into[i][0]) {
                         flag = false;
                         break;
                     }
                 }
                 if(flag) {
                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                     state = 1, step = 2;
                 }
                 break;
             }
             case 6: {//进入
                 if(into[rec_f][state]) {
                     printf("%02d:%02d %d people enter the elevator.\n", rec_t / 60, rec_t %60, into[rec_f][state]);
                     sum_into -= into[rec_f][state];
                     into[rec_f][state] = 0;
                     for(int i = 0; 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 = 3;//有人要进来就不关门
                 break;
             }
             case 7: {//离开
                 if(leave[rec_f]) {
                     printf("%02d:%02d %d people leave the elevator.\n", rec_t / 60, rec_t %60, leave[rec_f]);
                     sum_leave -= leave[rec_f];
                     leave[rec_f] = 0;
                     inc_time();
                 }
                 if(sum_leave == 0 && state == 1) {
                     bool flag = true;
                     for(int i = 0; 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 = 0;
                 }
                 else if(sum_leave == 0 && state == 0) {
                     bool flag = true;
                     for(int i = 0; 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 = 1;
                 }
                 step = 6;
                 break;
             }
             case 8: {//判断关门后动作
                 if(sum_into == 0 && sum_leave == 0) {//没人要进来没人在电梯里
                     step = 1;
                     continue;
                 }
                 if(sum_leave == 0) {//本层没人上,电梯没人,有请求
                     if(state == 1) {//是否继续向上
                         int i;
                         for(i = rec_f + 1; i <= 50; ++i) {
                             if(into[i][1] || into[i][0]) break;
                         }
                         if(i > 50) state = 0;
                     }
                     else {//是否继续向下
                         int i;
                         for(i = 1; i < rec_f; ++i) {
                             if(into[i][1] || into[i][0]) break;
                         }
                         if(i == rec_f) state = 1;
                     }
                 }
                 if(state) printf("%02d:%02d The elevator starts to move up from floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                 else printf("%02d:%02d The elevator starts to move down from floor %d.\n", rec_t / 60, rec_t %60, rec_f);
                 if(state) step = 4;
                 else step = 5;
                 break;
             }
         }
     }
     printf("%02d:%02d The elevator door is closing.\n", rec_t / 60, rec_t %60);
 }
 
 int main() {
     scanf("%d", &T);
     for(int t = 1; t <= T; ++t) {
         scanf("%d%d", &rec_f, &n);
         for(int i = 0; i < n; ++i) a[i].read();
         printf("Case %d:\n", t);
         solve();
         puts("");
     }
 }

 

解题转自:http://www.cnblogs.com/oyking/archive/2013/08/16/3262142.html


  1. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }