前言

全网唯一不同题解

设 \(f[i][j]\) 表示第 \(i\) 次选取留下来的数是 \(k\) 的最小花费

枚举前面的留下来的点 \(k\) 当前能留下的点只有 \((2*i),(2*i+1),k\) 中的一个,时间复杂度 \(O(n^2)\)

选取次数是 \(n/2\) 向上取整。因为最后什么点也不留下,\(a[n+1]=0\),所以答案可以为 \(f[m][n+1](m=n/2向上取整)\)

另外我没有写记录路径,记录一下也挺简单

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
inline int read() {
int x=0,f=1; char ch=getchar();
while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
return x * f;
}
const int N = 1007;
int n;
int a[N];
int f[N][N];
inline int Const(int x,int y) {
return max(a[x], a[y]);
}
int main()
{
n = read();
for(int i=1;i<=n;++i)
a[i] = read();
memset(f, 0x3f, sizeof(f));
f[1][1] = max(a[2],a[3]); f[1][2] = max(a[1],a[3]); f[1][3] = max(a[1],a[2]);
int m = n&1 ? n/2+1 : n/2;
for(int i=2;i<=m;++i) {
int x = 2*i, y = 2*i+1; // i次选取的最后两个数
for(int k=1;k<x;++k) { //枚举上一次留下的数
f[i][x] = min(f[i][x], f[i-1][k]+Const(y,k));
f[i][y] = min(f[i][y], f[i-1][k]+Const(x,k));
f[i][k] = min(f[i][k], f[i-1][k]+Const(x,y));
}
}
int ans = f[m][n+1];
printf("%d\n",ans);
return 0;
}

update

加上路径记录版

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
inline int read() {
int x=0,f=1; char ch=getchar();
while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
return x * f;
}
const int N = 1007;
int n;
int a[N];
int f[N][N];
struct operation {
int x,y,pre;
}p[N][N];
inline int Const(int x,int y) {
return max(a[x], a[y]);
}
void output(int now,int pos) {
if(now == 0) return ;
output(now-1,p[now][pos].pre);
if(a[p[now][pos].x])
printf("%d ",p[now][pos].x);
if(a[p[now][pos].y])
printf("%d",p[now][pos].y);
puts("");
}
int main()
{
n = read();
for(int i=1;i<=n;++i)
a[i] = read();
memset(f, 0x3f, sizeof(f));
f[1][1] = max(a[2],a[3]); f[1][2] = max(a[1],a[3]); f[1][3] = max(a[1],a[2]);
p[1][1] = (operation)<%2,3,0%>;
p[1][2] = (operation)<%1,3,0%>;
p[1][3] = (operation)<%1,2,0%>;
int m = n&1 ? n/2+1 : n/2;
for(int i=2;i<=m;++i) {
int x = 2*i, y = 2*i+1; // i次选取的最后两个数
for(int k=1;k<x;++k) { //枚举上一次留下的数
if(f[i-1][k]+Const(y,k) < f[i][x]) {
f[i][x] = f[i-1][k]+Const(y,k);
p[i][x] = (operation)<%k,y,k%>;
}
if(f[i-1][k]+Const(x,k) < f[i][y]) {
f[i][y] = f[i-1][k]+Const(x,k);
p[i][y] = (operation)<%k,x,k%>;
}
if(f[i-1][k]+Const(x,y) < f[i][k]) {
f[i][k] = f[i-1][k]+Const(x,y);
p[i][k] = (operation)<%x,y,k%>;
}
// f[i][x] = min(f[i][x], f[i-1][k]+Const(y,k));
// f[i][y] = min(f[i][y], f[i-1][k]+Const(x,k));
// f[i][k] = min(f[i][k], f[i-1][k]+Const(x,y));
}
}
int ans = f[m][n+1];
printf("%d\n",ans);
output(m,n+1);
return 0;
}

最新文章

  1. Android 知识杂记(MVP模式)
  2. EBS中启用OAF页面个性化三个配置
  3. Flash AS实现时钟效果(全脚本实现)
  4. MVC3项目依赖文件错误解决
  5. Saltstack之Syndic(十)
  6. 图结构练习——最短路径(dijkstra算法(迪杰斯拉特))
  7. PTA 07-图5 Saving James Bond - Hard Version (30分)
  8. java猜数字小游戏
  9. windows 多任务与进程
  10. 《android开发艺术探索》读书笔记(一)--Activity的生命周期和启动模式
  11. 使用sessionStorage、localStorage存储数组与对象
  12. springMVC工作过程
  13. Atitit 列表表格按照字段排序数据解决方案
  14. mysql执行计划查看工具explain
  15. ASP.net MVC模式介绍(一)
  16. VC2010中&quot;Include Directories&quot; 和 &quot;Additional Include Directories&quot;的区别
  17. Math(初学)
  18. Maven系列--setting.xml 配置详解
  19. [Cocos2d-x for WP8学习笔记] 获取系统字体
  20. SSM框架整合遇到的问题

热门文章

  1. spring bean.xml
  2. 深入理解JVM虚拟机1:JVM内存的结构与消失的永久代
  3. andriod\iphone视频禁止全屏播放
  4. onchange and oninput
  5. Cannot refer to the non-final local variable user defined in an enclosing scope
  6. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_3_数据结构_数组
  7. 刷题——一道全排列的题目(Permutations)
  8. PHP foreach &amp;$ 引发的bug
  9. CodeForces.1174D.EhabandtheExpectedXORProblem(构造前缀异或和数组)
  10. &lt;每日一题&gt; Day6:HDU递推专题完结