DP--类似最长上升子系列

 4214. 三元组 - AcWing题库

F数组的初始化也是十分关键的
F[0][0]表示在0个物品当中选了0个,价值肯定是0
F数组其余值应等于一个必不可能选择为答案的值

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 3010, INF = 0x3f3f3f3f;

int s[N], c[N], n;
int f[N][10];

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++ )   cin >> s[i];
    for(int i = 1; i <= n; i ++ )   cin >> c[i];
    
    memset(f, 0x3f, sizeof f);
    f[0][0] = 0;
    for(int i = 1; i <= n; i ++ )
        for(int j = 1; j <= 3; j ++ )
            for(int k = 0; k < i; k ++ )
                if(s[i] > s[k]) f[i][j] = min(f[i][j], f[k][j                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          - 1] + c[i]);

    
    int res = INF;
    for(int i = 1; i <= n; i ++ )   res = min(res, f[i][3]);
    if(res == INF)  res = -1;
    
    cout << res << endl;
    
    return 0;
}

posted @ 2022-05-05 08:41  光風霽月  阅读(12)  评论(0编辑  收藏  举报