题目大意:
  给你一个数列,从中选出k个互不重叠的非空子串,定义s[i]为第i个子串的和,求|s[1]-s[2]|+|s[2]-s[3]|+...+|s[k-1]-s[k]|的最大值。

思路:
  考虑将绝对值去掉,对于连续一段和单调的子串,结果只与其中峰值和谷值有关,中间的数会直接消掉。
  我们用f[i][j][0..3]表示在第i个数时总共选了j和子串时的状态。
  其中第三维为0时,表示当前子串为谷值。
  第三维为2时,表示当前子串为峰值。
  第三维为1时,表示当前子串在谷值到峰值之间。
  第三维为3时,表示当前子串在峰值到谷值之间。
  也不需要前缀和,因为你按每一个数DP,答案可以直接加上去。
  注意考虑边界情况,当j=1或k的时候只用加/减一次,当j!=1或k的时候,这个子串同时会时前、后两段的峰/谷值,要加/减两次。

 #include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) if(ch=='-') neg=true;
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return neg?-x:x;
}
const int _inf=-0x40000000;
const int N=,K=;
int a[N];
int f[N][K][];
int main() {
int n=getint(),k=getint();
for(register int i=;i<=n;i++) {
a[i]=getint();
}
for(register int j=;j<=k;j++) {
for(register int k=;k<=;k++) {
f[][j][k]=_inf;
}
}
for(register int i=;i<=n;i++) {
for(register int j=;j<=k;j++) {
const int s=a[i]*((j!=&&j!=k)?:);
f[i][j][]=std::max(f[i-][j][],f[i-][j-][])-s;//谷值
f[i][j][]=std::max(f[i-][j][],f[i][j][]);//谷值-峰值
f[i][j][]=std::max(f[i-][j][],f[i-][j-][])+s;//峰值
f[i][j][]=std::max(f[i-][j][],f[i][j][]);//峰值-谷值
if(j!=&&j!=k) {//上一段也在峰值和谷值之间
f[i][j][]=std::max(f[i][j][],f[i-][j-][]);
f[i][j][]=std::max(f[i][j][],f[i-][j-][]);
}
}
}
printf("%d\n",std::max(f[n][k][],f[n][k][]));
return ;
}

[CodeForces-513E2]Subarray Cuts的更多相关文章

  1. Codeforces 513E2 Subarray Cuts dp (看题解)

    我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...

  2. CodeForces 1187D Subarray Sorting

    Problem You are given an array \(a_1\),\(a_2\),-,\(a_n\) and an array \(b_1\),\(b_2\),-,\(b_n\). For ...

  3. [Codeforces513E2]Subarray Cuts

    Problem 给定一个长度为n的数字串,从中选取k个不重叠的子串(可以少选),将每个串求和si 求max|s1 - s2| + |s2 - s3| + ... + |sk - 1 - sk|(n & ...

  4. Rockethon 2015

    A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...

  5. Educational Codeforces Round 67 D. Subarray Sorting

    Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...

  6. Educational Codeforces Round 69 D. Yet Another Subarray Problem

    Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...

  7. CodeForces 1197D Yet Another Subarray Problem

    Time limit 2000 ms Memory limit 262144 kB Source Educational Codeforces Round 69 (Rated for Div. 2) ...

  8. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题

    C. Dominated Subarray Let's call an array

  9. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp

    D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...

随机推荐

  1. ES6基础知识汇总

    1.如何理解ECMAScript6? ECMAScript是什么,ECMASCript的作用 2.新增let关键字 let的用途 3.关键字const const作用,传址赋值 4.解构赋值 解构赋值 ...

  2. 2、Web基本介绍及Html语法介绍

    1.1 Web基本介绍 1.web就是world wide web的缩写.称之为全球广域网,俗称www.2.我们可以将web理解为就是当前的一种互联网.对于我们来说更多的就是网站服务.3.网站我们可以 ...

  3. 使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

    今天在使用maven打包项目时遇到一个错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin ...

  4. 工具===激活xmind 8

      [下载jar包]: https://stormxing.oss-cn-beijing.aliyuncs.com/files/XMindCrack.jar   方法: 打开xmind 8 安装目录的 ...

  5. 003iptables 命令介绍

    http://www.cnblogs.com/wangkangluo1/archive/2012/04/19/2457072.html iptables 防火墙可以用于创建过滤(filter)与NAT ...

  6. socket.io插件调用的demo

    1.利用socket.io插件制作一个聊天框,原理是输入对话,发送到服务,服务器在返回相应的对话,最后插入页面中,时间对话的功能,这里我是使用的node.js搭建的服务器. 附上源码 <!DOC ...

  7. aws rds

    1.还原快照,注意设置安全组的问题:不然会导致还原后连接不上:

  8. linux中getmntent setmntent endmntent 用法例子

    mntent 结构是在 <mntent.h> 中定义,如下:               struct mntent {                      char    *mnt ...

  9. Merge k Sorted Lists——分治与堆排序(需要好好看)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...

  10. day1 python简介和入门

    Linux的yum依赖自带Python,为防止错误,此处更新其实就是再安装一个Python: 安装Python 1.下载安装包     https://www.python.org/downloads ...