当前位置: 首页 > news >正文

P1063 学习笔记

省流:区间 DP 水题

题目传送门

顾名思义,“项链”说明这个题是一个环,我们可以使用类似 这题 的操作,断环为链,就是在原数组后面拼一截:

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

然后正常区间 DP 即可。

\(dp_{l,r}\) 表示合并 \([l,r]\) 这个区间所产生的能量最大值。

转移:

\[dp_{l,r}=\max_{k=i}^{j-1} (dp_{i,k}+dp_{k,j}+a_ia_ja_k) \]

套到代码里即可。

code
/*********************************************************** Author        : dingziyang888* Website       : https://www.luogu.com.cn/problem/* Created Time  :* FileName      :* Warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* 1.MLE?* 2.array size enough?* 3.long long?* 4.overflow long long?* 5.multiple task cleaned?* 6.freopen?* 7.TLE?* *******************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
#include <queue>
#include <string>
#include <cstring>
#include <set>
#include <bitset>
#include <unordered_set>
#include <vector>
#include <deque>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <list>
#include <array>
#include <iterator>
#include <cmath>
#include <new>
#include <random>
#include <cfloat>
#include <cstdlib>
#include <climits>
#include <numeric>
#include <complex>
#include <ctime>
#include <chrono>
#include <thread>
#include <mutex>
#include <future>
#include <exception>
#include <stdexcept>
#include <cstdint>
#include <cassert>
#include <stack>
#include <cctype>
#define DEBUG
#define Ofile(s) freopen(s".in", "r", stdin), freopen (s".out", "w", stdout)
#define Cfile(s) fclose(stdin), fclose(stdout)
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;using ll = long long;
using ull = unsigned long long;
using lb = long double;constexpr int mod = 998244353;
constexpr int maxn = 1005;int n, ans = -1;int a[maxn], dp[maxn][maxn];int main() {fast;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i], a[i + n] = a[i];for (int l = 2; l <= n + 1; l++)for (int i = 1; i + l - 1 <= n * 2; i++){int j = i + l - 1;for (int k = i + 1; k < i + l - 1; k++)dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[j] * a[k]);}for (int i = 1; i <= n; i++)ans = max(ans, dp[i][n + i]);cout << ans;return 0;
}
http://www.jsqmd.com/news/375702/

相关文章:

  • 【每日一题】LeetCode 3713. 最长的平衡子串 I
  • Java计算机毕设之基于springboot的小学生研学活动,游学活动管理系统基于springboot的小学生研学活动管理系统(完整前后端代码+说明文档+LW,调试定制等)
  • Java计算机毕设之基于Spring Boot的陶瓷文化网站的设计与实现基于springboot的陶瓷售卖系统(完整前后端代码+说明文档+LW,调试定制等)
  • 【毕业设计】基于springboot的小学生研学活动管理系统(源码+文档+远程调试,全bao定制等)
  • Java毕设项目推荐-基于springboot瓷器商城管理系统基于springboot的陶瓷售卖系统【附源码+文档,调试定制服务】
  • 读书笔记一:从“写代码”到“做工程”——个人能力与流程重塑
  • Java毕设项目推荐-基于Java springboot小学生研学管理系统考勤签到活动报名基于springboot的小学生研学活动管理系统【附源码+文档,调试定制服务】
  • 读书笔记二:团队协作——软件工程的核心命题
  • Java毕设选题推荐:基于springboot的小学生研学活动管理系统【附源码、mysql、文档、调试+代码讲解+全bao等】
  • 2026 年国产时序数据库技术深度解析:多模态融合架构与工程实践完整教程:从入门到实战部署
  • 2026Q1烟台财税公司县域征纳协同排行榜|深耕县域,适配税务便民服务,合规高效 - 品牌智鉴榜
  • Java毕设选题推荐:基于springboot的陶瓷售卖系统springboot瓷器商城管理系统【附源码、mysql、文档、调试+代码讲解+全bao等】
  • 基于微信小程序的智能垃圾分类助手系统
  • 读书笔记三:工程思维与职业素养——软件工程的底层逻辑
  • 基于SpringBoot和Vue的智慧城市管理中心平台
  • P1018 学习笔记
  • python super()方法和__class__变量
  • 基于SpringBoot和Vue的智慧医疗管理系统
  • 基于SpringBoot和Vue的政府集中采购管理系统设计与实现
  • Lab4-Lab: traps MIT6.1810操作系统工程【持续更新】 _
  • AI博主私藏|4款PPT工具实测,新手也能1小时出片(附避坑指南) - 品牌测评鉴赏家
  • AI博主实测|5款新手PPT工具,零门槛上手,告别熬夜排版 - 品牌测评鉴赏家
  • 芒格的“赢家的诅咒“提醒在高科技并购中的应用
  • 藏不住了,美妆博主实测TOP3,手动剃须刀闭眼冲!新手/敏感肌零踩雷 - 品牌测评鉴赏家
  • 操作系统安全必备技能:SELinux 操作指南
  • Lab3-Lab: traps MIT6.1810操作系统工程【持续更新】 _
  • 电商数据分析的自动化技术
  • 从理论到实践:大数据诊断性分析的完整教程
  • 美妆博主实测✅5款好用不贵手动剃须刀|平价封神不踩雷 - 品牌测评鉴赏家
  • 实测3款自动生成PPT工具|2026年AI博主私藏,打工人/程序员告别熬夜排版 - 品牌测评鉴赏家