当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

3016:K-Monotonic

题目描述

A sequence of integer numbers is called strictly monotonically increasing if every term of the sequence is strictly greater than the one preceding it. Similarly, a sequence is called strictly monotonically decreasing if every term is strictly less than the one preceding it. A strictly monotonic sequence is a sequence that is either strictly monotonically increasing or decreasing. A sequence of integers is called k-monotonic if it can be decomposed into k disjoint contiguous subsequences that are strictly monotonic.

For example a strictly monotonically increasing sequence is 1-monotonic — in fact it is k-monotonic for every k between 1 and the number of elements it contains. The sequence { 1, 2, 3, 2, 1 } is 2-monotonic since it can be decomposed into { 1, 2, 3 } and { 2, 1 }.

If a sequence is not k-monotonic, you can transform it into a k-monotonic sequence by performing the following operation one or more times: select any term in the sequence and either increase it or decrease it by one. You are allowed to perform any number of these operations on any of the terms. Given a sequence of numbers A1, A2, …, An and an integer k, you are to calculate the minimum number of operations required to transform the given sequence into a k-monotonic sequence.

输入解释

The input contains multiple test cases.

Each test case contains consists of two lines. The first line gives the integers n (1 ≤ n ≤ 1000) and k (1 ≤ k ≤ min{ n, 10 }). The second line gives the integers A1, A2, …, An (−100 000 ≤ Ai ≤ 100 000).

A pair of zeroes indicates the end of the input and should not be processed.

输出解释

Output the answer of each test case on a separate line.

输入样例
4 1
1 1 1 1
4 2
1 1 1 1
4 4
1 1 1 1
6 1
1 2 3 3 2 1
0 0
输出样例
4
2
0
9
来自北京大学POJ的附加信息
Case time limit(单组数据时间限制) 7000MS

该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-3016

最后修改于 2020-10-29T06:50:10+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
15000 262144