CodeForces - 1084A The Fair Nut and Elevator 数学

题目
The Fair Nut lives in n story house. ai people live on the i-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.

It was decided that elevator, when it is not used, will stay on the x-th floor, but x hasn’t been chosen yet. When a person needs to get from floor a to floor b, elevator follows the simple algorithm:

Moves from the x-th floor (initially it stays on the x-th floor) to the a-th and takes the passenger.
Moves from the a-th floor to the b-th floor and lets out the passenger (if a equals b, elevator just opens and closes the doors, but still comes to the floor from the x-th floor).
Moves from the b-th floor back to the x-th.
The elevator never transposes more than one person and always goes back to the floor x before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the a-th floor to the b-th floor requires |a−b| units of electricity.
Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the x-th floor. Don’t forget than elevator initially stays on the x-th floor.

题意:
找一个最省电的电梯停留楼层。已知楼高和每层楼有多少人,每个人每天用电梯2次

题解:
模拟实际,耗电量=电梯去接人+电梯送到目的地+电梯回停留楼层
让电梯分别停留在1-n层,计算每次的耗电量用d数组保存,最后找出数组中的最小值即可

代码

#include<iostream>
#include<cmath>
using namespace std;
int d[107];               //x设在每层楼所用的电
int main()
{
    int n;
    cin>>n;               //n层楼
    int a[107];           //每层多少人
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int j=1;j<=n;j++) //j为每次设定的x
    {
        int sum=0;
        for(int i=1;i<=n;i++)  //每层楼要耗得电数
        {
            sum+=(abs(j-i)+abs(i-1)+abs(j-1))*2*a[i];
        }
        d[j]=sum;
    }
    int t=100000000;
    for(int i=1;i<=n;i++)
    {
        if(d[i]<t)
            t=d[i];
    }
    cout<<t<<endl;
    return 0;
}

感谢观看!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值