[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 ...
随机推荐
- ArcFace2 #C 视频人脸比对教程
请允许我大言不惭,叫做教程,特希望各位能指正.哦,我用的是vs2017.使用虹软技术 一.准备工作1.创建项目 2.添加EMGU.CV包 3.复制虹软的dll到项目 ,并设属性“复制到输出目录”为“如 ...
- MYSQL去除"/r/n"
#去除回车符号/r/n UPDATE t_week_power_line_loss SET `line_loss_rate` = REPLACE( REPLACE( `line_loss_rate` ...
- mac软件
1. http://www.ifunmac.com/ 2.Mac安装软件时提示已损坏的解决方法 http://www.jianshu.com/p/3d04a2292fcd 3.mac以后有时间在装的软 ...
- 最常出现的字符串 Most Common Word
2018-10-26 00:32:05 问题描述: 问题求解: 方法一.Trie 最长出现的字符串,最容易想到的解法就是Trie树了,于是首先使用Trie树进行了实现,代码量有点大,当然了是可以A掉的 ...
- c# 静态构造函数与私有构造函数共存
在使用静态构造函数的时候应该注意几点: 1.静态构造函数既没有访问修饰符,也没有参数.因为是.NET调用的,所以像public和private等修饰符就没有意义了. 2.是在创建第一个类实例或任何静态 ...
- C# 读取word2003 并且显示在界面上的方法
1.新建一个windows窗体程序 2. 引入包WinWordControl.dll 3.添加引用 4.引入组件WinWordControl组件 5.主界面上加入按钮 ,opendialog, win ...
- ubuntu下安装Firefox中国版解决Ubuntu与Windows下Firefox账号同步问题(已解决)
1. 下载最新版本火狐Linux版 下载地址:http://firefox.com.cn/download/ 选择火狐Linux64-bit版,下载后文件为:Firefox-latest-x86_64 ...
- ActionCable的部署(参考Gorails)
Gorails视频 https://gorails.com/deploy/actioncable Action Cable – Integrated WebSockets for Rails http ...
- 《完美应用ubuntu》之全面管理ubuntu软件源
2.全面管理ubuntu软件包 2.1 DEB软件包之间的基本关系: (1)软件仓库:由ubuntu软件包的维护者维护并公开发布的DEB软件包的集合:可位于网络,服务器,硬盘等各种存储介质. (2)软 ...
- 『MXNet』第一弹_基础架构及API
MXNet是基础,Gluon是封装,两者犹如TensorFlow和Keras,不过得益于动态图机制,两者交互比TensorFlow和Keras要方便得多,其基础操作和pytorch极为相似,但是方便不 ...