5809: 傻瓜电梯(思维模拟)

5809: 傻瓜电梯

时间限制: 1 Sec   内存限制: 128 MB
提交: 196   解决: 35
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

所谓傻瓜电梯指的是在响应用户请求时缺乏相应的“智商”,在上升或下降的过程中不 能把中途的乘客捎带入电梯,而只会严格按照用户发出请求的先后顺序依次完成任务。
比如,原来电梯在 1 楼,首先 6 楼有一位乘客发出请求,要求由 6 楼乘坐到 10 楼去, 此时电梯马上会上去,但在电梯上升到 3 楼时,另外一位乘客请求由 5 楼乘坐到 8 楼去,傻 瓜电梯却不会在上升途中把 5 楼的乘客捎带上去,而只会先把 6 楼的乘客送到 10 楼,然后 再下来把 5 楼的乘客送到 8 楼。
傻瓜电梯由 i 楼上升到 i+1 楼(或下降到 i-1 楼)的时间都是 3 秒,每到达一个楼层, 不管进出乘客有多少,也不管乘客只有进、只有出或者进出电梯都有,所耽搁的时间都是 6 秒。现在味味要根据傻瓜电梯接受到的 n 个用户请求,编程计算傻瓜电梯把所有乘客送到目 标楼层时总共所需要的时间。
如果某批乘客到达目标楼层后,电梯没有马上要响应的请求,则电梯在前一批乘客的 目的地等待,这个等待时间也需计入总花费时间。直到下一批乘客发出新请求,电梯才会从 当前位置出发,前往下一批乘客的出发楼层。

输入

输入第一行包含两个整数 x(1<=x<=100)和 n(1<=n<=100),分别表示 傻瓜电梯开始所在的楼层和总共接收到的请求数目。下面有 n 行,每行包含 3 个整数,依次 表示该请求发出的时间、乘客目前所在的楼层和将要去的目标楼层。其中请求发出的时间以 秒为时刻单位,最大可能的值是 2000。如果某两个请求的发出时间相同,则按照输入中原始的先后顺序依次处理。

输出

输出只包含一行一个整数,表示傻瓜电梯把所有乘客送到目标楼层后总共所需要的时间(从得到第一条请求时开始计算时间),单位是秒。

样例输入

3 4
10 10 2
18 1 9
2 1 12
8 6 10

样例输出

162

提示

第一批乘客发出请求到离开电梯所需时间:3*2+6+3*11+6=51 从前一批乘客离开电梯到第二批乘客离开电梯所需时间:3*6+6+3*4+6=42 第三批乘客从出发地出发到离开电梯所需时间:
3*8+6=30(由于出发地与前一批乘客目的地相同,所以上下客时间不必再加 6) 从前一批乘客离开电梯到第四批乘客离开电梯所需时间:3+6+3*8+6=39 总花费时间:51+42+30+39=162

来源

第27届宁波市信息学竞赛小学组 

依晰当时比赛的时候被这个题坑惨了!!!题意不明确啊。。。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct node{
    int time, x, y, num;
}people[105];
int cmp(node a, node b)
{
    return a.time<b.time;
}
int main()
{
    int x, n;
    long long temp=0, sum=0, t=0;
    scanf("%d%d", &x, &n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d", &people[i].time, &people[i].x, &people[i].y);
    //for(int i=1;i<n;i++)
    //    people[i].time-=people[0].time;
    sort(people, people+n, cmp);
    for(int i=0;i<n;i++)
    {
        if(sum<people[i].time && i!=0)
            sum=people[i].time;
        temp=0;
        temp+=abs(x-people[i].x)*3;
        if(x!=people[i].x || i==0)
            temp+=6;
 
        temp+=abs(people[i].x-people[i].y)*3;
        temp+=6;
 
        sum+=temp;
        x=people[i].y;
    }
    printf("%lld\n", sum);
    return 0;
}
 
/**************************************************************
    Problem: 5809
    User: ldu_reserver201701
    Language: C++
    Result: 正确
    Time:0 ms
    Memory:1096 kb
****************************************************************/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++源代码 注重类的交互 片段 #include using namespace std; #include "elevator.h" //Elevator class definition #include "person.h" //Person class definition #include "floor.h" //Floor class definition //constants that represent time required to travel //between floors and direction of the elevator const int Elevator::ELEVATOR_TRAVEL_TIME = 5; const int Elevator::UP = 0; const int Elevator::DOWN = 1; //constructor Elevator::Elevator( Floor &firstFloor, Floor &secondFloor) : elevatorButton( * this ), currentBuildingClockTime( 0 ), moving( false ), direction( UP ), currentFloor( Floor::FLOOR1 ), arrivalTime( 0 ), floor1NeedsService( false ), floor2NeedsService( false ), floor1Ref( firstFloor ), floor2Ref( secondFloor ), passengerPtr( 0 ) { cout << "elevator constrcuted" <<endl; }// end Elevator constructor //destructor Elevator::~Elevator() { delete passengerPtr; cout << "elevator destructed" << endl; }//end Elevator destructor //give time to elevator void Elevator::processTime( int time ) { currentBuildingClockTime = time; if ( moving ) //elevator is moving processPossibleArrival(); else processPossibleDeparture(); if ( !moving ) cout << "elevator at rest on floor " << currentFloor << endl; }// end function processTime // when elevator is moving, determine if it should stop void Elevator::processPossibleArrival() { //if elevator arrives at destination floor if ( currentBuildingClockTime == arrivalTime ) { currentFloor = ( currentFloor == Floor::FLOOR1 ? Floor::FLOOR2 : Floor::FLOOR1); //update current floor direction = ( currentFloor == Floor::FLOOR1 ? UP : DOWN ); //update direction cout << "elevator arrives on floor " << currentFloor <<endl; // process arrival at currentFloor arriveAtFloor( currentFloor == Floor::FLOOR1 ? floor1Ref : floor2Ref); return; }//end if //elevator still moving cout << "elevator moving " << ( direction == UP ? "UP" : "DOWN"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhjian6666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值