[CodeForces-513E2]Subarray Cuts
题目大意:
给你一个数列,从中选出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的更多相关文章
- Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...
- 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 ...
- [Codeforces513E2]Subarray Cuts
Problem 给定一个长度为n的数字串,从中选取k个不重叠的子串(可以少选),将每个串求和si 求max|s1 - s2| + |s2 - s3| + ... + |sk - 1 - sk|(n & ...
- Rockethon 2015
A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
- CodeForces 1197D Yet Another Subarray Problem
Time limit 2000 ms Memory limit 262144 kB Source Educational Codeforces Round 69 (Rated for Div. 2) ...
- Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题
C. Dominated Subarray Let's call an array
- 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 \( ...
随机推荐
- MyBatis笔记之配置输出日志并打印输出SQL语句
1. 引入Log4J的Maven依赖: <dependency> <groupId>log4j</groupId> <artifactId>log4j& ...
- [转载]Android中Bitmap和Drawable
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- shell编程===执行shell脚本的四种方法
使用vim创建一个shell文件,命名 hello.sh #!/bin/bash echo "hello shell !" 在linux中进行加载 chmod +x ./hello ...
- linux删除第几天日志【原创】
cat del.sh #!/bin/bash thirty=`date -d '30days ago' +%Y-%m-%d` cd $ #删除输入路径下第30天的日志文件 find . -name & ...
- Apache+jboss群集部署
Jboss default方式上的Cluster配置[二] - 操作系统http://www.myexception.cn/operating-system/862858.html Jboss def ...
- [How to] 使用HBase协处理器---基本概念和regionObserver的简单实现
1. 简介 对于HBase的协处理器概念可由其官方博文了解:https://blogs.apache.org/hbase/entry/coprocessor_introduction 总体来说其包含两 ...
- BootStrap的栅格系统的基本写法(布局)
代码如下: <!DOCTYPE html> <html> <head> <title>BootStrap的基础入门</title> < ...
- HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...
- 结构体对齐及#pragma详细解释
在linux下c语言结构体对齐: 1.自然对齐 struct 是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float 等)的变量,也可以是一些复合数据类型(如array.s ...
- Button Bashing(搜索)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx8AAAI2CAIAAAC+EqK4AAAgAElEQVR4nOydf0BT9f7/37fS423mWn