题意:

思路:

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std; const int N = , INF = -0x3f3f3f3f;
int n, m, ans[N], v[N], a[N], last[N][N], f[N][N][N], g[N][N][N];
map<int, int> hash; inline void upt(int &x, const int &y) {
if (x < y) x = y;
} char ch;
inline int read() {
int res = , sgn = ;
while (ch = getchar(), ch < '' || ch > '') if (ch == '-') break;
ch == '-' ? sgn = : res = ch - ;
while (ch = getchar(), ch >= '' && ch <= '') res = res * + ch - ;
return sgn == ? res : -res;
} int main() {
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout); n = read();
for (int i = ; i <= n; ++i) v[i] = read();
for (int i = ; i <= n; ++i) {
a[i] = read();
if (hash[a[i]] == ) hash[a[i]] = ++m;
}
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) last[i][j] = last[i - ][j];
last[i][hash[a[i]]] = i;
}
memset(f, INF, sizeof(f));
memset(g, INF, sizeof(g));
for (int i = ; i <= n; ++i) {
for (int j = ; j <= n; ++j)
if (i + j <= n)
f[i][i][j] = g[i][i][j] = v[j + ];
else break;
f[i][i - ][] = g[i][i - ][] = ;
}
for (int l = ; l <= n; ++l)
for (int i = ; i <= n; ++i) {
int j = i + l;
if (j > n) break;
for (int k = ; k <= n; ++k) {
if (j + k > n) break;
upt(f[i][j][k], f[i][j - ][] + v[k + ]);
upt(g[i][j][k], f[i][j - ][] + v[k + ]);
int p = hash[a[j] + ], q = hash[a[j] - ];
for (int h = last[j][p]; h >= i; h = last[h - ][p])
upt(f[i][j][k], f[i][h][k + ] + f[h + ][j - ][]);
for (int h = last[j][q]; h >= i; h = last[h - ][q]) {
upt(f[i][j][k], g[i][h][k + ] + f[h + ][j - ][]);
upt(g[i][j][k], g[i][h][k + ] + f[h + ][j - ][]);
}
}
}
ans[] = ;
for (int i = ; i <= n; ++i) {
upt(ans[i], ans[i - ]);
for (int j = ; j < i; ++j)
upt(ans[i], ans[j] + f[j + ][i][]);
}
printf("%d\n", ans[n]);
return ;
}

【ZJOI2017 Round1练习】D8T2 sequence(DP)的更多相关文章

  1. 【ZJOI2017 Round1练习&BZOJ4767】D1T3 两双手(排列组合,DP)

    题意: 100%的数据:|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500 思路:听说这是一道原题 只能往右或者下走一步且有禁止点的简化版 ...

  2. 【ZJOI2017 Round1练习】D4T2 trie(贪心,状压DP)

    题意:现在 Matej 手上有 N 个英文小写字母组成的单词, 他想知道,如果将这 N 个单词中的字母分别进行重新排列,形成的字母树的节点数最少是多少. n<=16,len[i]<=100 ...

  3. 【ZJOI2017 Round1练习&BZOJ4774】D3T2 road(斯坦纳树,状压DP)

    题意: 对于边带权的无向图 G = (V, E),请选择一些边, 使得1<=i<=d,i号节点和 n − i + 1 号节点可以通过选中的边连通, 最小化选中的所有边的权值和. d< ...

  4. 【ZJOI2017 Round1练习&BZOJ5354】D7T3 room(DP)

    题意: 思路: 写了两种版本 考场版本 ..,..]of longint; t:..,..]of longint; n,m,i,j,k,oo,ans,d1:longint; function min( ...

  5. 【ZJOI2017 Round1练习&BZOJ5350】D5T1 masodik(DP,斜率优化)

    题意:你要从(0,0)点走到(n,m), 每次只能往 x 轴或者 y 轴正方向移动一个单位距离.从(i,j)移动到(i,j+1)的代价为 ri,从(i,j)移动到(i+1,j)的代价为 cj. 求最小 ...

  6. 【ZJOI2017 Round1练习&UVA1057】D6T1 Routing(DP,SPFA)

    题意:给你一个有向图, 并指定起点和终点. 问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点. 对于 100%的数据, 有 N<=100, M<=min(1000,N* ...

  7. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  8. Codeforces Round #277 (Div. 2) E. LIS of Sequence DP

    E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...

  9. hdu6078 Wavel Sequence dp+二维树状数组

    //#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...

随机推荐

  1. ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)

    问题详情 来源是,我在Ambari集群里,安装Hue. 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解 所遇到的这个问题. 然后,去ambari-server的log日志,查看,如下 ...

  2. leetcode764 Largest Plus Sign

    思路: 首先使用dp计算出在每个位置(i, j)上下左右最多有多少个连续的1,得到up[i][j], down[i][j], left[i][j], right[i][j].然后计算这四个值中的最小值 ...

  3. jquery 实现 点击把数据移动右侧 点击再次移回到左侧

    2018年第一发  希望新的一年和大家一下学习更多知识    JS://把数据左边挪到了右边,再从右边移动回来function moveOption(e1, e2){   $("#" ...

  4. ReactJS-0-React介绍

    React介绍: React是一个库而不是一个MVC框架,因为React只负责解决MVC框架中V(View)层面的问题,React致力于创建可重用的UI组件.(React is a library f ...

  5. sh/bash/csh/Tcsh/ksh/pdksh等shell本质区别

    sh/bash/csh/Tcsh/ksh/pdksh等shell本质区别 1. Shell脚本的书写 在写Shell脚本时,往往第一行要注明用什么解释器来解释这个脚本. 如#!/bin/bash即用/ ...

  6. java.lang.String 字符串操作

    1.获取文件名 //获取文件名,即就是去掉文件的后缀 /** * mypic.jpg * 获取文件名 * 1. 先找到"."的位置 * 2. 从第一个字符开始截取到".& ...

  7. js 日期时间大小比较

    <body> 开始时间:<input onfocus="setday(this)" id="startTime" name="sta ...

  8. t-sql的楼梯:超越基本级别6:使用案例表达式和IIF函数

    t-sql的楼梯:超越基本级别6:使用案例表达式和IIF函数 源自:Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expre ...

  9. 自定义对话框(jDialog)

    [配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...

  10. 360浏览器 收藏夹 ico 缓存 目录

    C:\Users\Administrator\AppData\Roaming\360se6\apps\data\users\default\data\ico