PAT Advanced 1008. Elevator

发布时间 2023-05-07 16:15:56作者: 十豆加日月

PAT Advanced 1008. Elevator

1. Problem Description:

The highest building in our city has only one elevator. A request list is made up with \(N\) positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

2. Input Specification:

Each input file contains one test case. Each case contains a positive integer \(N\), followed by \(N\) positive numbers. All the numbers in the input are less than 100.

3. Output Specification:

For each test case, print the total time on a single line.

4. Sample Input:

3 2 3 1

5. Sample Output:

41

6. Performance Limit:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

要被自己蠢哭了QAQ,可能是昨晚没睡好。一开始没看清题意,以为题目没有给出数字个数(想到根据EOF结束输入),并且每层楼只被访问一次,这样才正好符合测试样例(我还想说为啥测试样例对不上2333)。第一次提交时除了testpoint5都报wrong answer,检查后才发现输入有给出数字个数,并且不是每层楼只能访问一次,按照给定楼层顺序计算,维护好上一楼层的记录即可,修改后AC。

My Code & Result:

// #include <iostream>

// #define MAX_NUM 100
// #define UP_COST 6
// #define DOWN_COST 4
// #define STAY_COST 5

// using namespace std;

// // first submit testpoint 0-4, 6-9 wrong answer
// int main(void)
// {
//     int tempNum=0;
//     int visit[MAX_NUM] = {0};
//     int lastFloor = 0;
//     int cost = 0;

//     visit[0] = 1; // start from 0th floor
//     while(scanf("%d", &tempNum) != EOF) // use this to end input
//     {
//         ++visit[tempNum]; // assume visit this floor
        
//         if(visit[tempNum] == 1) // first visit
//         {
//             if(tempNum > lastFloor) // move up
//             {
//                 cost += (tempNum-lastFloor) * UP_COST;
//             }
//             else // move down
//             {
//                 cost += (lastFloor-tempNum) * DOWN_COST;
//             }
            
//             cost += STAY_COST;
//             lastFloor = tempNum; // update lastFloor
//         }
        
//         // printf("%d ", tempNum);
//     }

//     printf("%d\n", cost);

//     return 0;
// }


#include <iostream>

#define UP_COST 6
#define DOWN_COST 4
#define STAY_COST 5

using namespace std;

// first submit testpoint 0-4, 6-9 wrong answer
int main(void)
{
    int tempNum=0;
    int lastFloor = 0;
    int cost = 0;
    int numCount=0;
    int i=0; // iterator
    
    scanf("%d", &numCount);
    for(i=0; i<numCount; ++i)
    {
        scanf("%d", &tempNum);
        if(tempNum > lastFloor) // move up
        {
            cost += (tempNum-lastFloor) * UP_COST;
        }
        else // move down or stay
        {
            cost += (lastFloor-tempNum) * DOWN_COST;
        }
        
        cost += STAY_COST;
        lastFloor = tempNum; // update lastFloor
    }
    
    printf("%d\n", cost);
    
    return 0;
}
Compiler
C++ (g++)
Memory
468 / 65536 KB
Time
10 / 400 ms