AGC035D Add and Remove

题意

给出\(n\)个数,每次删除一个不在两端的数,然后把它的权值加到相邻的两个数上。

问操作\(n-2\)次后,所剩的两数之和的最小值

\(n\le18\)

题解

暴力存储每一个数的状态肯定不行。

考虑计算每一个数被计算了多少次。

可以发现\(1\)和\(n\)一定只被计算了1次

最后一个被消除掉的数应只被计算\(2\)次

可以发现,如果左端点被计算\(x\)次,右端点被计算\(y\)次

那么左右端点之间最后一个被消除的数被计算了\(x+y\)次

然后就可以开始记忆化搜索了

注意全部都用\(map\)存储状态会\(TLE\),需要把其中一部分用数组存储

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz=20;
int n;
struct node{
int l,r,fl,fr;
const bool operator<(const node &p)const{
if(l!=p.l) return l<p.l;
if(r!=p.r) return r<p.r;
if(fl!=p.fl) return fl<p.fl;
if(fr!=p.fr) return fr<p.fr;
}
};
ll a[sz];
ll g[sz][sz][611][611];
map<node,ll>dp;
ll f(int l,int r,int fl,int fr){
if(l+1==r) return 0;
if(fl<611&&fr<611){
if(g[l][r][fl][fr]) return g[l][r][fl][fr];
}
else if(dp[(node){l,r,fl,fr}]) return dp[(node){l,r,fl,fr}];
ll ret=1e18;
ll sum=fl+fr;
for(int i=l+1;i<=r-1;i++){
ret=min(ret,sum*a[i]+f(l,i,fl,sum)+f(i,r,sum,fr));
}
if(fl<611&&fr<611) return g[l][r][fl][fr]=ret;
else return dp[(node){l,r,fl,fr}]=ret;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
printf("%lld\n",a[1]+f(1,n,1,1)+a[n]);
}

AGC035D的更多相关文章

  1. @atcoder - AGC035D@ Add and Remove

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 N 张排成一行的卡片,第 i 张卡片上面写着 Ai. 重复 ...

  2. 【AGC035D】Add and Remove(脑洞 DP 分治)

    题目链接 大意 给出\(N\)个数的序列,每次操作可以选择连续的三个数,将中间的那个数抽出,将另外两个数的数值加上中间那个数的数值. 一直执行以上操作直到只剩最后两个数,求最后两个数的所有可能的和的最 ...

随机推荐

  1. python Six 模块

    Six模块用于python2和python3的兼容 import six 官网链接:https://six.readthedocs.io/

  2. <day002>Selenium基本操作+unittest测试框架

    任务1:Selenium基本操作 from selenium import webdriver # 通用选择 from selenium.webdriver.common.by import By # ...

  3. 帝国cms学习

    手册地址1 手册地址2 入门 安装: 将下载的upload里的文件上传到网站更目录 然后 域名/e/install/index.php Warning: Use of undefined consta ...

  4. SpringCloud学习笔记《---01 概念 ---》篇

  5. Android开发 处理拍照完成后的照片角度

    private void initImageAngle(){ Bitmap imageBitmap = BitmapFactory.decodeFile(FilePathSession.getFace ...

  6. 工作中遇到的bug

    1. Error: No PostCSS Config found in.. 在项目根目录新建postcss.config.js文件,并对postcss进行配置: module.exports = { ...

  7. session过期跳转到登陆页面并跳出iframe框架的两个方法

    最近在做拦截器,判断用户登录后操作超时,失去权限然后要重新登录,但是用的iframe,返回的登陆页总是在框架中显示,我百度了下,总是只有其中一个方法,现在分享下两种解决方法,希望对你们有帮助: 方法一 ...

  8. slam课程

    美国宾夕法尼亚大学最近有录制一套 无人机视觉定位导航相关的视频课程,2019年3月份在YouTube上更新完毕,质量非常高,名字叫Robotics,视频课程列表:https://www.youtube ...

  9. http response 过长 导致Connection reset

    http response 过长(2W byte) 导致Connection reset

  10. PAT甲级——A1071 Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...