Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以
把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn
然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中间, 在底部到顶部的中间。
反思一下为什么没写出来:我在考虑的时候 |s2 - s1| + |s3 - s2| + |s4 + s3| ,,, 我在想怎么把si 和 si + 1的大小关系的问题,
其实不用考虑, 因为我们求的是最大值, 所以对答案是没有影响的。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, K, a[N]; int dp[N][][]; int main() {
cin >> n >> K;
for(int i = ; i <= n; i++) cin >> a[i];
memset(dp, 0xc0, sizeof(dp));
for(int i = ; i <= n; i++)
for(int k = ; k < ; k++)
dp[i][][k] = ;
for(int i = ; i <= n; i++) {
for(int j = ; j <= K; j++) {
int cnt = j == || j == K ? : ;
dp[i][j][] = max(dp[i - ][j][], dp[i - ][j - ][]) - cnt * a[i];
dp[i][j][] = max(dp[i - ][j][], dp[i][j][]);
dp[i][j][] = max(dp[i - ][j][], dp[i - ][j - ][]) + cnt * a[i];
dp[i][j][] = max(dp[i - ][j][], dp[i][j][]);
if(cnt == ) {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - ][]);
dp[i][j][] = max(dp[i][j][], dp[i - ][j - ][]);
}
}
}
cout << max(dp[n][K][], dp[n][K][]) << "\n";
return ;
} /*
*/
Codeforces 513E2 Subarray Cuts dp (看题解)的更多相关文章
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)
第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...
- Codeforces 442D Adam and Tree dp (看题解)
Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...
- Codeforces 1101F Trucks and Cities dp (看题解)
Trucks and Cities 一个很显然的做法就是二分然后对于每个车贪心取check, 这肯定会TLE, 感觉会给人一种贪心去写的误导... 感觉有这个误导之后很难往dp那个方向靠.. dp[ ...
- Codeforces 596D Wilbur and Trees dp (看题解)
一直在考虑, 每一段的贡献, 没想到这个东西能直接dp..因为所有的h都是一样的. #include<bits/stdc++.h> #define LL long long #define ...
- Codeforces 983C Elevator dp (看题解)
Elevator 怎么今天写啥题都不会写啊, 我是傻了吗.. 把电梯里面四个人的目标点当作状态, 然后暴力转移. #include<bits/stdc++.h> #define LL lo ...
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
- Codeforces 498B Name That Tune 概率dp (看题解)
Name That Tune 刚开始我用前缀积优化dp, 精度炸炸的. 我们可以用f[ i ][ j ] 来推出f[ i ][ j + 1 ], 记得加加减减仔细一些... #include<b ...
随机推荐
- ansible笔记(9):常用模块之包管理模块
ansible笔记():常用模块之包管理模块 yum_repository模块 yum_repository模块可以帮助我们管理远程主机上的yum仓库. 此处我们介绍一些yum_repository模 ...
- 缓存系列之一:buffer、cache与浏览器缓存
缓存系列之一:buffer.cache与浏览器缓存 一:缓存是为了调节速度不一致的两个或多个不同的物质的速度,在中间对速度较快的一方起到一个加速访问速度较慢的一方的作用,比如CPU的一级.二级缓存是保 ...
- python时间日期格式化和反格式化
strftime()和strptime()行为 date,datetime和time对象都支持一种 strftime(format)方法,以创建一个表示显式格式字符串控制下的时间的字符串.从广义上讲, ...
- C#方法的重写
问题一:什么是重写? “重写”父类方法就是修改它的实现方式或者说在子类中对它进行重新编写. 问题二:为什么要重写父类的方法 通常,子类继承父类的方法,在调用对象继承方法的时候,调用和执行的是 ...
- 分析Vue框架源码心得
1.在封装一个公用组件,比如button按钮,我们多个地方使用,不同类型的button调用不同的方法,我们就可以这样用 代码片段: <lin-button v-for="(item,i ...
- Java测试代码(很不完整,建议大家别看,过几天会再发一次难的版本)
package ATM; import java.io.BufferedReader; import java.io.InputStreamReader; class Account{ priv ...
- 1005:Number Sequence(hdu,数学规律题)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- Python随手记—各种方法的使用
os.popen()方法的使用 os.popen()方法用于从一个命令打开一个管道. 语法:os.popen(command[, mode[, bufsize]]) 其中 command是使用的 ...
- IDEA的debug操作
- 使用android-ndk官方ndkbuild例子
Why this blog 现在(2018年9月27日),Android Studio中新建ndk项目都使用cmake而不是Android.mk+Application.mk的方式.但老项目需要维护, ...