题意:

思路:

 #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. iOS9 关于明文HTTP报错的修复方法

    报错:App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. ...

  2. Java_JDBC连接数据库

    package com.accp.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pre ...

  3. c#中stringbuilder的方法总结

    String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新 ...

  4. hihocoder编程练习赛52-2 亮灯方案

    思路: 状态压缩dp.实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ; ] = {, , ...

  5. ReactJS-2-props vs state

    rops理解: 大多数组件都可以在创建的时候被不同的参数定制化,这些不同的参数就叫做props.props的流向是父组件到子组件. 子组件Comment,是一条评论组件,父组件CommentList, ...

  6. php高效率对一维数组进行去重

    $input = array("a" => "green", "red", "b" => "gre ...

  7. InChatter系统开源聊天模块前奏曲

    最近在研究WCF,又因为工作中的项目需要,要为现有的系统增加一个聊天模块以及系统消息提醒等,因此就使用WCF做服务器端开发了一个简单的系统. 开发最初学习了东邪孤独大哥的<传说的WCF系列> ...

  8. JavaScript——responseType

    https://www.cnblogs.com/cdemo/p/5225848.html https://blog.csdn.net/wkyseo/article/details/78232485 异 ...

  9. Vue.js语法糖整理

    el:element 需要获取的元素,一定是HTML中的根容器元素 data:用于数据的存储 methods:用于存储各种方法 数据绑定字面量只加载一次{{* msg}} data里面可以进行简单的运 ...

  10. jquery日期控件+时分秒

    因为项目需要,一些时间上的查询要精确的时分.先看下效果图吧. 所需要的js 跟css 文件 jsp://特别注意引入的先后顺序 <link rel="stylesheet" ...