【ZJOI2017 Round1练习】D8T2 sequence(DP)
题意:


思路:


#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)的更多相关文章
- 【ZJOI2017 Round1练习&BZOJ4767】D1T3 两双手(排列组合,DP)
题意: 100%的数据:|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500 思路:听说这是一道原题 只能往右或者下走一步且有禁止点的简化版 ...
- 【ZJOI2017 Round1练习】D4T2 trie(贪心,状压DP)
题意:现在 Matej 手上有 N 个英文小写字母组成的单词, 他想知道,如果将这 N 个单词中的字母分别进行重新排列,形成的字母树的节点数最少是多少. n<=16,len[i]<=100 ...
- 【ZJOI2017 Round1练习&BZOJ4774】D3T2 road(斯坦纳树,状压DP)
题意: 对于边带权的无向图 G = (V, E),请选择一些边, 使得1<=i<=d,i号节点和 n − i + 1 号节点可以通过选中的边连通, 最小化选中的所有边的权值和. d< ...
- 【ZJOI2017 Round1练习&BZOJ5354】D7T3 room(DP)
题意: 思路: 写了两种版本 考场版本 ..,..]of longint; t:..,..]of longint; n,m,i,j,k,oo,ans,d1:longint; function min( ...
- 【ZJOI2017 Round1练习&BZOJ5350】D5T1 masodik(DP,斜率优化)
题意:你要从(0,0)点走到(n,m), 每次只能往 x 轴或者 y 轴正方向移动一个单位距离.从(i,j)移动到(i,j+1)的代价为 ri,从(i,j)移动到(i+1,j)的代价为 cj. 求最小 ...
- 【ZJOI2017 Round1练习&UVA1057】D6T1 Routing(DP,SPFA)
题意:给你一个有向图, 并指定起点和终点. 问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点. 对于 100%的数据, 有 N<=100, M<=min(1000,N* ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- 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 ...
- hdu6078 Wavel Sequence dp+二维树状数组
//#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...
随机推荐
- 调用wsdl接口,参数是xml格式
1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...
- iOS Programming Touch Events and UIResponder
iOS Programming Touch Events and UIResponder 1 Touch Events As a subclass of UIResponder, a UIView ...
- mvc框架 与vuex的介绍
应用级的状态集中放在store中: 改变状态的方式是提交mutations,这是个同步的事物: 异步逻辑应该封装在action中. const vuex_store = new Vuex.store( ...
- 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...
- Record these plug-ins of vscode
实在无聊透顶.写个随笔记录一下vscode的插件好了. 第一次使用(之前一直在用sublime...),以后再更新吧.record my color too! Visual Studio Code B ...
- SQL GROUP BY 和 ORDER BY 区别
order by 是按表中某字段排列表中数据group by 是按某些字段分类. 例如按 1.按年龄排序表中的记录select * from users order by age 2.按年龄分类表中数 ...
- CAD参数绘制点(网页版)
点在CAD中的作用除了可以分割对象外,还能测量对象,点不仅表示一个小的实体,而且通过点作为绘图的参考标记. pdmode是一个控制point的形式的系统变量,当pdmode=0时是可见的一个点,当pd ...
- E. String Multiplication
E. String Multiplication time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- vue中的main.js打开直接报错问题解决
安装好后也是报这样的错,是因为WebStorm默认使用的是ES5的语法,改成ES6就可以了. 解决方案:
- python selenium等待特定网页元素加载完毕
selenium等待特定元素加载完毕 is_disappeared = WebDriverWait(driver, 8, 0.5, ignored_exceptions=TimeoutExceptio ...