#P12988. [GCJ 2022 #1B] Controlled Inflation

    ID: 12787 Type: RemoteJudge 5000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>动态规划 DP2022Google Code Jam

[GCJ 2022 #1B] Controlled Inflation

题目描述

The lines at the air pump at your gas station are getting too long! You want to optimize the process to help customers more quickly inflate their tires, sports balls, giant parade balloon animals, and other products.

The pump is automatic: you set the pressure to a specific number of pascals and plug the pump into the inflatable product, and it will inflate as needed to that exact pressure. There are only two buttons on the pump: up and down. They increase and decrease the target pressure, respectively, by exactly 11 pascal.

There is a line of N\mathbf{N} customers, each of whom brings exactly P\mathbf{P} products that they need to get inflated by the pump. You know the target pressure of each product. You can inflate the products from a customer in any order you want, but you cannot change the order of the customers. Specifically, you must inflate all products from the ii-th customer before inflating any from the (i+1)(i+1)-th customer. In between handling two products, if those two products have different target pressures, you need to use the buttons on the pump.

The pump is initially set to 0 pascals, and it can be left at any number after all products of all customers have been inflated. If you order the products of each customer optimally, what is the minimum number of button presses you need?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line containing two integers, N\mathbf{N} and P\mathbf{P}: the number of customers and the number of products each customer brings, respectively. Then, N\mathbf{N} lines follow. The ii-th of these lines contains P\mathbf{P} integers $\mathbf{X}_{\mathbf{i}, 1}, \mathbf{X}_{\mathbf{i}, 2}, \ldots, \mathbf{X}_{\mathbf{i}, \mathbf{P}}$, representing that the jj-th product that the ii-th customer brings has a target pressure of Xi,j\mathbf{X}_{\mathbf{i}, \mathbf{j}} pascals.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of button presses needed to inflate all products according to their specified pressures.

2
3 3
30 10 40
20 50 60
60 60 50
5 2
1 1000000000
500000000 1000000000
1 1000000000
500000000 1
1 1000000000
Case #1: 110
Case #2: 4999999996

提示

Sample Explanation

In Sample Case #1, an optimal way to use the pump is:

  1. press up 10 times, setting the pump to 10; pump the product (from customer 1) that needs 10 pascals,
  2. press up 30 times, setting the pump to 40; pump the product (from customer 1) that needs 40 pascals,
  3. press down 10 times, setting the pump to 30; pump the product (from customer 1) that needs 30 pascals,
  4. press down 10 times, setting the pump to 20; pump the product (from customer 2) that needs 20 pascals,
  5. press up 30 times, setting the pump to 50; pump the product (from customer 2) that needs 50 pascals,
  6. press up 10 times, setting the pump to 60; pump the product (from customer 2) and the two products (from customer 3) that need 60 pascals, and finally
  7. press down 10 times, setting the pump to 50; pump the product (from customer 3) that needs 50 pascals.

This is a total of 110 button presses.

In Sample Case #2, notice that the answer can be larger than 2322^{32}.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • $1 \leq \mathbf{X}_{\mathbf{i}, \mathbf{j}} \leq 10^{9}$, for all i,ji, j.

Test Set 1 (14 Pts, Visible Verdict)

  • 2N102 \leq \mathbf{N} \leq 10.
  • 2P32 \leq \mathbf{P} \leq 3.

Test Set 2 (21 Pts, Hidden Verdict)

  • 2N10002 \leq \mathbf{N} \leq 1000.
  • 2P1002 \leq \mathbf{P} \leq 100.