[Codeforces513E2]Subarray Cuts
Problem
给定一个长度为n的数字串,从中选取k个不重叠的子串(可以少选),将每个串求和si
求max|s1 - s2| + |s2 - s3| + ... + |sk - 1 - sk|(n <= 30000, k <= min(n, 200))
Solution
绝对值后的和,只和峰值和谷值的那些值有关(所以我们可以贪心峰值和谷值尽量多)
用f[i][j][k]表示前i个,分成j段,这个值在哪里(用k=0表示在谷值,k=1表示在谷值到峰值之间,k=2表示在峰值,k=3表示在峰值到谷值之间)
f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j - 1][3]) - flag * x;
f[i][j][1] = max(f[i - 1][j][1], f[i][j][0]);
f[i][j][2] = max(f[i - 1][j][2], f[i - 1][j - 1][1]) + flag * x;
f[i][j][3] = max(f[i - 1][j][3], f[i][j][2]);
flag是什么呢?如果是第一个或者最后一个计算时只算一次,中间的都算两次
然后实际中,全部是峰值和谷值是不一定现实的,所以中间部分还要加上转移:
f[i][j][1] = max(f[i][j][1], f[i - 1][j - 1][1]);
f[i][j][3] = max(f[i][j][3], f[i - 1][j - 1][3]);
Notice
注意第一个或最后一个和其他地方是不一样的。
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 30005, K = 205;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int f[N][K][4];
int sqz()
{
int n = read(), k = read();
rep(i, 1, k)
rep(j, 0, 3) f[0][i][j] = -INF;
rep(i, 1, n)
{
int x = read();
rep(j, 1, k)
{
int flag = 2 - (j == 1 || j == k);
f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j - 1][3]) - flag * x;
f[i][j][1] = max(f[i - 1][j][1], f[i][j][0]);
f[i][j][2] = max(f[i - 1][j][2], f[i - 1][j - 1][1]) + flag * x;
f[i][j][3] = max(f[i - 1][j][3], f[i][j][2]);
if (flag - 1)
{
f[i][j][1] = max(f[i][j][1], f[i - 1][j - 1][1]);
f[i][j][3] = max(f[i][j][3], f[i - 1][j - 1][3]);
}
}
}
printf("%d\n", max(f[n][k][1], f[n][k][3]));
}
[Codeforces513E2]Subarray Cuts的更多相关文章
- [CodeForces-513E2]Subarray Cuts
题目大意: 给你一个数列,从中选出k个互不重叠的非空子串,定义s[i]为第i个子串的和,求|s[1]-s[2]|+|s[2]-s[3]|+...+|s[k-1]-s[k]|的最大值. 思路: 考虑将绝 ...
- Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...
- Rockethon 2015
A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- unbuntu安装Node.js
在官网https://nodejs.org/en/下载 手动创建链接的话,新安装的angular的ng typescript的tsc都要自己手动建立软链接,要不就每个工程里npm install一 ...
- const修饰函数
#include <iostream> using namespace std; class A { public: A(int age); void printAge() const; ...
- Codeforces 1005 F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...
- python Django 创建应用
如图输入如下命令 python manage.py startapp apitest 添加应用到 autotest项目项目下 在settings.pyo 中加入“apitest”,如下图 创建视图 在 ...
- 推送消息 web push notification
参考 : https://developers.google.com/web/fundamentals/engage-and-retain/push-notifications/ ( step b ...
- jquery如何获取checkbox的值
jquery如何获取checkbox的值 一.总结 一句话总结:就是通过jquery获取哪些对应name的checkbox,然后找出:check(被选中的),然后通过jquery的each遍历获取这些 ...
- python 断言大全
参考链接:https://blog.csdn.net/qq1124794084/article/details/51668672 1. 小数位模糊等于 自动化脚本最重要的是断言,正确设置断言以后才能帮 ...
- 确认OHS版本的方法
还是 opatch lsinventory 好用 C:\Oracle\Middleware\ohs\OPatch>opatch lsinventory Oracle Interim Patch ...
- toad 快捷键大全
现在在企业中,操作oracle数据库的客户端,除了PL/SQL外,使用的较多的就是TOAD了!toad记得F9执行全部sql和Ctrl + Enter执行选中sql就行了,有啥需要的再查吧.快捷设置在 ...
- tomcat ----> 源码关联/编译/....
今天在搞Servlet时想看下tomcat Servlet-API.jar的源码,按照惯性思维用以往关联SSH2框架的源码的方式去做但是结果没有成功,尝试过换不同的文件夹,起初怀疑可能是路径太深关联不 ...