Luo Gu P2800 and the Suoyao

Topic Portal

Problem-solving ideas:

With f [i] [0] is not to represent the i-th layer using magic, and f [i] [1] is represented by the minimum fly magic i-th layer.

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 int n,a[1000002],f[1000002][2];
 7 
 8 int main() {
 9     scanf("%d",&n);
10     for(int i = 1;i <= n; i++)
11         scanf("%d",&a[i]);
12     f[2][0] = a[1];
13     f[2][1] = 0;
14     for(int i = 3;i <= n + 1; i++) {
15         f[i][0] = min(f[i-1][0],f[i-1][1]) + a[i-1];
16         f[i][1] = min(f[i-1][0],f[i-2][0]);
17     }
18     printf("%d",min(f[n+1][0],f[n+1][1]));
19     return 0;
20 }

 

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/12000016.html