前言

全网唯一不同题解

设 \(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;
}

CF 82 D.Two out of Three的更多相关文章

  1. Android shape与selector标签使用

    原文地址:Android shape与selector标签使用 Android中提供一种xml的方式,让我们可以自由地定义背景,比较常用的就是shape标签和selector标签 shape shap ...

  2. [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)

    A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...

  3. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  6. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  7. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  8. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  9. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

随机推荐

  1. 北风设计模式课程---访问者(Visitor)模式

    北风设计模式课程---访问者(Visitor)模式 一.总结 一句话总结: 设计模式是日常问题的经验总结方案,所以学好设计模式对日常出现的问题可以有很好的解决. 访问者设计模式有点神似 抽象工厂模式, ...

  2. 大牛整理最全Python零基础入门学习资料

    大牛整理最全Python零基础入门学习资料 发布时间:『 2017-11-12 11:56 』     帖子类别:『人工智能』  阅读次数:3504 (本文『大牛整理最全Python零基础入门学习资料 ...

  3. Collector 源码分析

    Collectors Collectors 配合 stream 可以实现 MapReduce 操作,也可以单独完成流中元素的收集. 收集器接口和实现 /** * 收集器接口 */ public int ...

  4. Http Handler 介绍

    引言 在 Part.1 Http请求处理流程 一文中,我们了解了Http请求的处理过程以及其它一些运作原理.我们知道Http管道中有两个可用接口,一个是IHttpHandler,一个是IHttpMod ...

  5. python接口自动化:对外接口sign签名

    签名参数sign生成的方法: 在接口开发过程中,一般通过时间戳+sign作为密匙加密传输 实现代码如下: #python实现sign签名 import hashlib,time class sign: ...

  6. python-backports.ssl-match-hostname 安装问题

    转载请标明本文链接:(https://www.cnblogs.com/softwarecb/p/python-backports-ssl.html) 系统版本Ubuntu14.04 因为要用Conta ...

  7. BZOJ 4987 (树形DP)

    ###题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4987 ###分析 先考虑贪心,显然k个节点形成一棵树 求出树的直径,显然直径应该只被经 ...

  8. exec 命令

    source source命令即点(.)命令. 在bash下输入man source,找到source命令解释处,可以看到解释”Read and execute commands from filen ...

  9. python内置函数sorted()及sort() 函数用法和区别

    python内置函数sorted(),sort()都有排序的意思,但是两者有本质的区别,sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作,list 的 sort ...

  10. BZOJ 1908. Pku2054 UVA1205 Color a Tree

    传送门 看一眼感觉 $dp$,发现状态没法维护 考虑贪心,然后就想了两个错的贪心... 正解好神啊 首先如果权值最大的点能够一步染色那么肯定要染它 意思就是,一旦父节点被染色那么它就要接着被染色 那么 ...