CF 82 D.Two out of Three
前言
全网唯一不同题解
设 \(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的更多相关文章
- Android shape与selector标签使用
原文地址:Android shape与selector标签使用 Android中提供一种xml的方式,让我们可以自由地定义背景,比较常用的就是shape标签和selector标签 shape shap ...
- [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 ...
- 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 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- 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 ...
- 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 ...
随机推荐
- [CSP-S模拟测试]:小P的生成树(数学+Kruskal)
题目描述 小$P$是个勤于思考的好孩子,自从学习了最大生成树后,他就一直在想:能否将边权范围从实数推广到复数呢?可是马上小$P$就发现了问题,复数之间的大小关系并没有定义.于是对于任意两个复数$z_1 ...
- MySQL5.6transportable tablespace
https://blog.csdn.net/xiaoyi23000/article/details/53150776
- LinkedHashMap 源码分析
LinkedHashMap LinkedHashMap 能解决什么问题?什么时候使用 LinkedHashMap? 1)LinkedHashMap 按照键值对的插入顺序进行遍历,LinkedHashM ...
- CSS镜像
https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scaleX transform: scaleX(-1);/*左 ...
- GMS认证测试FQA
---摘要 本文档用于收录GMS认证测试的异常问题,提供一般性指导.对于本文档中未提供解答的问题请咨询@开发经理或@领域技术专家 cts测试工具如何获取? A:见Google官网 https://so ...
- fiddler之简单的接口性能测试
在针对某一个/某一些接口,发送相同的请求,不考虑参数的变化时,可以使用fiddler进行简单的性能测试.(使用功能为:replay) 一.replay功能调用 (1.Reissue Requests: ...
- Where we love is home, home that our feet may leave, but not our hearts.
parcel.n. 包裹 endurance.n.耐力 rot.v.腐烂 ornament.n.装饰 pinch.v.捏 nationality.n.国家 sunshine.n.阳光 stagger. ...
- Mac013--Docker安装
一.Docker安装教程 参考:http://www.runoob.com/docker/macos-docker-install.html 可应用brew命令安装,也可自定义下载安装. 应用brew ...
- ODBC Driver Development
ODBC Driver Development By Vikash Agarwal, May 01, 2002 Open your database system to the world. Vika ...
- Spring框架中AOP特性
1.AOP介绍 即:面向切面编程,在不改变原有方法的定义与使用.也不改变原程序流程的情况下,可以改变原有方法的功能{增加一些附加的功能,在指定的地方添加其他函数方法:} 2.其他的方法:[需要的四个接 ...