Discussion area
Magical Girl
Time limit: 1000 ms | Memory limit: 65535 KB
Difficulty: 3
Description
A few days ago, Xu Yuanxuan’s Juxian Madoka was really on fire. When Hei Changzhi (Xiaoyan) climbed up the stairs to fight the Witch's Night, she encountered a problem and wanted your help. Because Witch’s Night was suspended in the air, she had to climb the stairs. There were n floors in the ruins, and each floor was different in height, which caused Xiaoyan to take a different time to climb each floor. But of course, Xiao Yan knows time magic, and can fly through one or two layers instantly [ie not time-consuming]. But every time she teleported, she had to climb at least one more layer (adding magic power at this time) before she could use teleporting again. It takes Xiaoyan 1 second to climb each unit height. Eliminating the night of the witch is urgent, so Xiao Yan wants you to help her find a plan in the shortest time to reach the top of the building.
Input
This question has multiple sets of data, and ends with the file input.
A number N (1 <= N <= 10000) in the first row of each group of data represents the number of floors.
Next N lines, each line has a number H (1 <= H <= 100), which represents the height of this layer.
Output
For each group of data, output one line, a number S, which represents the shortest time required to reach the top of the building.
Sample input
5
3
5
1
8
4
Sample output
1
#include<iostream>
using namespace std;
int h[10005];
int dp[10005][2];
int main() {
//1表示飞
//0表示不飞
int layers;
while (cin >> layers) {
for (int i = 1; i <= layers; i++) {
cin >> h[i];
}
dp[1][1] = 0;
dp[1][0] = h[1];
dp[2][1] = 0;
dp[2][0] = h[2];
for (int i = 3; i <= layers; i++) {
dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + h[i];
dp[i][1] = min(dp[i - 2][0], dp[i - 1][0]);
}
cout << min(dp[layers][0], dp[layers][1]) << endl;
}
return 0;
}