【NOIP考前题目回顾】Luogu P1040
来源:互联网 发布:淘宝直播怎么秒杀下单 编辑:程序博客网 时间:2024/06/06 02:14
思路
第一问看上去像是个树形结构,但是其实我们可以把它直接看成是一个区间DP。令
代码
#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <queue>#include <utility>int nextInt(){ int num = 0; char c; bool flag = false; while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n'); if (c == '-') flag = true; else num = c - 48; while (std::isdigit(c = std::getchar())) num = num * 10 + c - 48; return (flag ? -1 : 1) * num;}typedef long long LL;LL n, f[31][31] = { 0 }, i, j, k, x, a[31] = { 0 }, node[31][31] = { 0 };void LTR(const int l, const int r){ if (l > r) return; std::cout << node[l][r] << ' '; LTR(l, node[l][r] - 1); LTR(node[l][r] + 1, r);}int main(int argc, char **argv){ int n = nextInt(); for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) f[i][j] = 1; for (int i = 1; i <= n; i++) { a[i] = nextInt(); f[i][i] = a[i]; node[i][i] = i; } for (int i = n - 1; i >= 1; i--) for (int j = i + 1; j <= n; j++) for (int k = i; k <= j; k++) if (f[i][k - 1] * f[k + 1][j] + a[k] > f[i][j]) { node[i][j] = k; f[i][j] = f[i][k - 1] * f[k + 1][j] + a[k]; } std::cout << f[1][n] << std::endl; LTR(1, n);#ifdef __EDWARD_EDIT std::cin.get(); std::cin.get();#endif return 0;}
阅读全文
0 0
- 【NOIP考前题目回顾】Luogu P1040
- 【NOIP考前题目回顾】Luogu P1000
- 【NOIP考前题目回顾】Luogu P1001
- 【NOIP考前题目回顾】Luogu P1003
- 【NOIP考前题目回顾】Luogu P1005
- 【NOIP考前题目回顾】Luogu P1006
- 【NOIP考前题目回顾】Luogu P1005
- 【NOIP考前题目回顾】Luogu P1012
- 【NOIP考前题目回顾】Luogu P1046
- 【NOIP考前题目回顾】Luogu P1047
- Luogu考前模拟Round2总结
- NOIP 考前模板整理
- NOIP 考前注意
- NOIP考前的心得体会
- NOIP 考前错误总结
- NOIP考前总结
- noip考前大总结
- NOIP 考前提醒
- webclient详解
- php GD库使用
- EF的常用知识积累
- 【二分+最小生成树】BZOJ2654[tree]题解
- 开源大数据周刊-第70期
- 【NOIP考前题目回顾】Luogu P1040
- 求数组元素的和
- ucos ii 文件分析
- BZOJ 1880 [Sdoi2009]Elaxia的路线
- C学习笔记--fputc()与fgetc()
- 扫雷游戏
- freemarker 使用记录
- web.xml 文件配置 标签解释
- 【编译原理】语法分析(一)