【CodeForces 987C】Three displays
【链接】 我是链接,点我呀:)
【题意】
【题解】
动态规划 设dp[i][j]表示前i个数字,选了j个的最小花费。 dp[i][j] = min(dp[k][j-1]+b[i]);//其中a[i]>a[k]且ka[k]的位置k才有可能。 复杂度O(N^2)【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3;
const int INF = 4e8;
int n;
int a[N+10],b[N+10];
int dp[N+10][4];
int main()
{
#ifdef LOCAL_DEFINE
freopen("rush.txt","r",stdin);
#endif // LOCAL_DEFINE
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) cin >> b[i];
for (int i = 1;i <= 3;i++)
for (int j = 1;j <= n;j++)
dp[j][i] = INF;
for (int i = 1;i <= n;i++) dp[i][1] = b[i];
for (int j = 2;j <= 3;j++)
for (int i = j;i <= n;i++)
for (int k = j-1;k <= i-1;k++)
if (a[i]>a[k] && dp[i][j]>dp[k][j-1]+b[i]){
dp[i][j] = dp[k][j-1]+b[i];
}
int ans = INF;
for (int i = 3;i <= n;i++)
ans = min(ans,dp[i][3]);
if (ans==INF)
cout<<-1<<endl;
else
cout<<ans<<endl;
return 0;
}