In the building where Xiao Ming works, there is only one elevator, and during rush hours, there is often a long queue. In the morning, because he is afraid of being late, he always chooses to take the stairs to save time.
One day, Xiao Ming came across a report stating that the elevator consumes 3 units of electricity when going up one floor and 2 units when going down. Seeing the significant electricity consumption of the elevator, environmentally conscious Xiao Ming decided to start taking the stairs from then on.
Xiao Ming was calculating in his mind that the elevator operates from the first floor every day, going up and down numerous times. He wondered how much electricity the elevator consumes in a day. Could you help him write a program to calculate it?
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input is an integer N (2 ≤ N ≤ 5000), representing the number of times the elevator goes up or down in a day. The second line contains N positive integers X.i (1 ≤ Xi ≤ 1000,1 ≤ i ≤ N) 表示電梯停留的樓層。 | Output a single line representing the total electricity consumption of the elevator for the day. |
4 4 5 6 7 | 18 |
7 8 4 5 2 10 4 6 | 80 |
8 21 19 17 15 13 11 9 7 | 88 |
Thought Process
You can start by declaring a variable to represent the current position of the elevator, initially set to 1. Each time you receive floor data, compare it with the current position. If the elevator is going up, add (new floor - current floor) * 3 to the answer. If it's going down, add (current floor - new floor) * 2 to the answer. Of course, you can also use abs to perform the subtraction directly.
Sample Code-ZeroJudge G497: Elevator
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N, pos = 1, ans = 0;
cin >> N;
for (int i = 0; i<N; i++)
{
int tmp;
cin >> tmp;
if (tmp > pos)
{
ans += (tmp-pos) * 3;
pos = tmp;
}
else
{
ans += (pos-tmp) * 2;
pos = tmp;
}
}
cout << ans << "\n";
}
//ZeroJudge G497
//Dr. SeanXD