#P13391. [GCJ 2010 #1A] Make it Smooth

    ID: 15260 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>动态规划 DP2010单调队列Google Code Jam

[GCJ 2010 #1A] Make it Smooth

题目描述

You have a one-dimensional array of NN pixels. Each pixel has a value, represented by a number between 0 and 255, inclusive. The distance between two pixels is the absolute difference of their numbers.

You can perform each of the following operations zero or more times:

  1. With cost DD, delete any pixel, so its original neighbors become neighboring pixels.
  2. With cost II, insert one pixel of any value into any position -- either between two existing pixels, or before the first pixel, or after the last pixel.
  3. You can change the value of any pixel. The cost is the absolute difference of the old value of the pixel and the new value of the pixel.

The array is smooth if any neighboring pixels have distance at most MM. Find the minimum possible cost of a sequence of operations that makes the array smooth.

Note: The empty array -- the array containing no pixels -- is considered to be smooth.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow, each with two lines. The first line is in the form "D I M N", the next line contains NN numbers aia_i: the values of the pixels from left to the right.

输出格式

For each test case, output one line containing "Case #x: y", where xx is the case number (starting from 1), and yy is the minimum cost to make the input array smooth.

2
6 6 2 3
1 7 5
100 1 5 3
1 50 7
Case #1: 4
Case #2: 17

提示

Sample Explanation

In Case #1, decreasing the 77 to 33 costs 44 and is the cheapest solution. In Case #2, deleting is extremely expensive; it's cheaper to insert elements so your final array looks like $[1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 50, 45, 40, 35, 30, 25, 20, 15, 10, 7]$.

Limits

  • All the numbers in the input are integers.
  • 1T1001 \leqslant T \leqslant 100
  • 0D,I,M,ai2550 \leqslant D, I, M, a_i \leqslant 255

Small dataset (12 Pts, Test set 1 - Visible)

  • 1N31 \leqslant N \leqslant 3.

Large dataset (24 Pts, Test set 2 - Hidden)

  • 1N1001 \leqslant N \leqslant 100.