atcoder A - Frog 1(DP)
A - Frog 1
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :?100100?points
Problem Statement
There are?NN?stones, numbered?1,2,…,N1,2,…,N. For each?ii?(1≤i≤N1≤i≤N), the height of Stone?ii?is?hihi.
There is a frog who is initially on Stone?11. He will repeat the following action some number of times to reach Stone?NN:
- If the frog is currently on Stone?ii, jump to Stone?i+1i+1?or Stone?i+2i+2. Here, a cost of?|hi?hj||hi?hj|?is incurred, where?jj?is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone?NN.
Constraints
- All values in input are integers.
- 2≤N≤1052≤N≤105
- 1≤hi≤1041≤hi≤104
Input
Input is given from Standard Input in the following format:
NN h1h1 h2h2 …… hNhNOutput
Print the minimum possible total cost incurred.
Sample Input 1?Copy
Copy 4 10 30 40 20Sample Output 1?Copy
Copy 30If we follow the path?11?→?22?→?44, the total cost incurred would be?|10?30|+|30?20|=30|10?30|+|30?20|=30.
Sample Input 2?Copy
Copy 2 10 10Sample Output 2?Copy
Copy 0If we follow the path?11?→?22, the total cost incurred would be?|10?10|=0|10?10|=0.
Sample Input 3?Copy
Copy 6 30 10 60 10 60 50Sample Output 3?Copy
Copy 40If we follow the path?11?→?33?→?55?→?66, the total cost incurred would be?|30?60|+|60?60|+|60?50|=40|30?60|+|60?60|+|60?50|=40.
?
題目鏈接:https://atcoder.jp/contests/dp/tasks/dp_a
題意:給你一堆石頭,每一個石頭有一個高度,有一只青蛙站在第一個石頭上,青蛙每一次可以跳1-2個石頭,并且產生起跳高度和落地高度的差的消耗。
問你青蛙跳到第N個石頭,最小需要消耗多少能量?
?
思路:
簡單的線性DP, 定義dp[i]的狀態意義為青蛙跳到第i個石頭的時候消耗的最小能量,
轉移方程即為:dp[i]=min(dp[i-2]+abs(a[i]-a[i-2]),dp[i-1]+abs(a[i]-a[i-1]))
初始狀態定義: dp[1] = 0 ,? dp[2]=| a[2]-a[1] |
dp[2]一定要預處理,狀態轉移只能從i=3開始,因為第二個石頭只能由第一個石頭跳過去。
不這樣定義會wa的。(親測,23333)
我的AC代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define gg(x) getInt(&x) using namespace std; typedef long long ll; inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ ll n; ll dp[maxn]; ll a[maxn]; int main() {gbtb;cin>>n;repd(i,1,n){cin>>a[i];}dp[1]=0;dp[0]=0;dp[2]=abs(a[2]-a[1]);repd(i,3,n){dp[i]=min(dp[i-2]+abs(a[i]-a[i-2]),dp[i-1]+abs(a[i]-a[i-1]));}cout<<dp[n];return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }?
轉載于:https://www.cnblogs.com/qieqiemin/p/10247378.html
總結
以上是生活随笔為你收集整理的atcoder A - Frog 1(DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 1-9-假期训练心得(dp+bfs)
- 下一篇: 300 页干货!李宏毅《一天搞懂深度学习
