PAT Advanced 1008. Elevator
PAT Advanced 1008. Elevator
1. Problem Description:
The highest building in our city has only one elevator. A request list is made up with
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
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)