题目传送门:https://acm.hznu.edu.cn/OJ/problem.php?id=1503
题解:我们发现后一状态由前一状态决定,即后一公里由前面十公里的状态决定,经典 dp,我们直接列出状态转移方程:dp[1]=a[1],dp[i]=min(dp[i],dp[j]+a[i-j]),i-j<=10。
点击查看代码
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10;
int a[11] = {0};
int dp[510] = {0};
int main(void)
{
Zeoy;
for (int i = 1; i <= 10; ++i)
cin >> a[i];
int n;
cin >> n;
dp[1] = a[1];
for (int i = 2; i <= n; ++i)
{
dp[i] = inf;
for (int j = i - 1; j >= 0 && i - j <= 10; j--)
{
dp[i] = min(dp[i], dp[j] + a[i - j]);
}
}
cout << dp[n] << endl;
return 0;
}