D. Learning to Paint
1.P3957 [NOIP2017 普及组] 跳房子2.P2034 选择数字3.P1714 切蛋糕4.E - Insert or Erase5.P4147 玉蟾宫6.P2866 [USACO06NOV] Bad Hair Day S7.P2032 扫描8.P1884 [USACO12FEB] Overplanting S9.P5905 【模板】全源最短路(Johnson)10.P1801 黑匣子11.P2168 [NOI2015] 荷马史诗12.P8306 【模板】字典树13.P1481 魔族密码14.P4341 [BJWC2010] 外星联络15.P1470 [USACO2.3] 最长前缀 Longest Prefix16.P3384 【模板】重链剖分/树链剖分17.P10288 [GESP样题 八级] 区间18.P2161 [SHOI2009] 会场预约19.P1878 舞蹈课20.P9691 [GDCPC2023] Base Station Construction21.P3611 [USACO17JAN] Cow Dance Show S22.F. Cutting Game23.G. Money Buys Less Happiness Now24.D. In Love25.F. Selling a Menagerie
26.D. Learning to Paint
27.P4588 [TJOI2018] 数学计算28.P3522 [POI2011] TEM-Temperature题解
dp+多次优先队列
设
则
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;
}
合集:
数据结构
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~