前言

全网唯一不同题解

设 \(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. SQL GROUP BY两个列

    首先group by 的简单说明: group by 一般和聚合函数一起使用才有意义,比如 count sum avg等,使用group by的两个要素:   (1) 出现在select后面的字段 要 ...

  2. [django]上下文管理器

    上下文管理器django提取context中的数据去供模板调用 需求: 所有的页面都需要一个特定的变量 本质: python函数 , 接收一个HttpRequest对象的参数 , 且返回的必须是一个字 ...

  3. 一台电脑多个git使用 push 时候出现denied

    http://my.oschina.net/silentboy/blog/220158 当一台电脑上多个git account 的时候, 出现如下问题, $ git push origin maste ...

  4. Linux内核调试方法总结之ltrace

    ltrace [用途] 库文件调用跟踪器,Linux内核内建命令,用法类似strace [命令格式] [参数说明][详细说明参考man ltrace帮助文档] -D 打印调试信息 01-DEBUG_G ...

  5. vue+ts修改父组件属性的写法。

    部分代码如下: 父组件:   <coupon  :modifyFlag.sync="flag" />    data() {     return {       fl ...

  6. 004-windows(64位)下使用curl命令

    一.下载工具包:http://curl.haxx.se/download.html 二.使用 使用方式一:在curl.exe目录中使用 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的 ...

  7. deepin 安装tar.gz

    由于网上推荐的比较多的安装方式是:sudo apt-get install mysql-server mysql-client,这个安装的是mysql5.7,既然都安装了就要安装最新的,所以从官网下载 ...

  8. C++ 命名管道示例

    想做一个 Hook CreateFile 重定向到内存的功能,貌似可以假借命名管道实现这个功能.不熟悉命名管道,做了几个demo,如下: Server: // NamedPipeServer.cpp ...

  9. shell 比较符号

    if [ 1 -ne 1 ];then...fi这是指当1不等于1时执行then后的语句 -eq:等于-ne:不等于-le:小于等于-ge:大于等于-lt:小于-gt:大于

  10. vue复合组件----注册表单

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...