D. Learning to Paint

原题链接

题解

dp+多次优先队列

dp[i][1,i] 区间内,前 k 个最大值(有可能不足k个)(注意 dp[i] 是一个序列)

dp[i]={dp[j][t]+a[j+2][i],j[0,i2],t[0,topj]},t=k

code

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int a[1005][1005];

struct Compare {
    bool operator()(const array<int, 4>& a, const array<int, 4>& b) {
        return a[3] < b[3];
    }
};

void solve()
{
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++) cin >> a[i][j];


    vector<vector<int>> dp(n + 5);
    dp[0].push_back(0);

    for (int i = 1; i <= n; i++)
    {
        priority_queue<array<int, 4>, vector<array<int, 4>>, Compare> q;

        q.push({i+1, i - 1, 0, dp[i - 1][0]});

        for (int j = i; j >= 1; j--)  q.push({j, max(0,j - 2), 0, a[j][i] + dp[max(0,j-2)][0]});


        while (!q.empty() && dp[i].size() < k)
        {
            auto [l, it, cnt, val] = q.top();
            q.pop();

            dp[i].push_back(val);
            if (cnt +1< dp[it].size()) q.push({l, it, cnt + 1, dp[it][cnt+1] + a[l][i]});
        }
    }


    for(auto it:dp[n]) cout<<it<<' ';cout<<'\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
    return 0;
}

posted @   纯粹的  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示