题目
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;
}
感谢观看!