首页 > 其他分享 >PAT Advanced 1008. Elevator

PAT Advanced 1008. Elevator

时间:2023-05-07 16:24:07浏览次数:34  
标签:PAT int lastFloor tempNum Elevator COST define Advanced cost

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

标签:PAT,int,lastFloor,tempNum,Elevator,COST,define,Advanced,cost
From: https://www.cnblogs.com/tacticKing/p/17379467.html

相关文章

  • [Typescript] Builder pattern 07- Reducer
    import{Expect,Equal}from"../types/utils"import{expect,it}from'vitest';typePayloadsToDiscriminatedUnion<TextendsRecord<string,any>>={[KinkeyofT]:{type:K}&T[K];}[keyofT];exportclas......
  • 关于Kubernetes-v1.23.6-初始化时报错[WARNING FileExisting-tc]: tc not found in sy
    今天笔者在部署Kubernetes-v1.23.6版本时,在对master节点使用如下命令进行初始化时,报错:[WARNINGFileExisting-tc]:tcnotfoundinsystempath当然其实也从字符意义上来看,只能算是WARNING提醒,不会影响主要的功能和结果,但既然有这个提醒,就可能就在某些地方是有轻微影响的,为了......
  • PAT Advanced 1007. Maximum Subsequence Sum
    PATAdvanced1007.MaximumSubsequenceSum1.ProblemDescription:Givenasequenceof\(K\)integers{\(N_1,N_2,...,N_K\)}.Acontinuoussubsequenceisdefinedtobe{\(N_i,N_{i+1},...,N_j\)}where\(1≤i≤j≤K\).TheMaximumSubsequencei......
  • C. Ehab and Path-etic MEXs
    C.EhabandPath-eticMEXs对于成链的情况,\(\text{MEX}=n-1\)一般的,一定有一条路径包含0和1,则可以确定\(\text{MEX}\geq2\),观察发现,对于度数\(\geq3\)的点,我们在他的三条边赋值为0,1,2使得其他路径的边有:0,1,...0,2,...1,2,...即一条路径上的边不能同时有0,1,......
  • 一统天下 flutter - 存储: path_provider - 用于获取不同平台的本地存储的路径
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-存储:path_provider-用于获取不同平台的本地存储的路径示例如下:lib\storage\path_provider.dart/**path_provider-用于获取不同平台的本地存储的路径**在pubspec.yaml中做如......
  • Error creating bean with name ‘dataSource‘ defined in class path resource解决
    原因是导入了jdbc的依赖,使用@Configuration注解向spring注入了dataSourcebean。但是因为工程中没有关于dataSource相关的配置信息,当spring创建dataSourcebean因缺少相关的信息就会报错。有两个办法:办法1:去除spring-boot-starter-jdbc的依赖或者mybatis的依赖办法2:在Sprin......
  • [BUG]multiprocessing/connection.py OSError:AF_UNIX path too long EOFError
       解决方法,当前代码的路径太长了,把路径变得短一些就可以了......
  • npm ERR! code EPERM npm ERR! syscall mkdir npm ERR! path C:\Program Files\node
    npm项目初始化代码npminit--yesidea代码安装npmnpmiexperss我输入的时候报错了,如下图所示没关系,只需要手动打开C盘的路径文件找到这个文件,并且把他Ctrl+D删除掉即可之后在运行这串代码就可以啦明显成功了......
  • cpp: Strategy Pattern II
     //Gold.h:此文件包含"Gold"类。策略模式StrategyPatternC++14//2023年5月1日涂聚文GeovinDuVisualStudio2022edit.#pragmaonce//#ifndefGOLD_H//#defineGOLD_H#ifndef_GOLD_#define_GOLD_#include<iostream>#include<sstrea......
  • Linux配置添加自定义shell脚本需要的PATH
    Linux添加自定义shell脚本记录下,便于之后复习使用。1.确定一个目录e.g.#到达用户目录cd~#创建一个bin文件夹来放脚本文件mkdirbincd./binpwd得到的是/root/bin2.把这个路径放到PATH中cd~#可以用ls-a看一看有没有.branrc文件vim~/.bashrc#编辑最后加入......