#P6831. [IOI 2020] 嘉年华奖券

    ID: 5871 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2020递归IOI交互题Special Judge

[IOI 2020] 嘉年华奖券

Description

Ringo is attending a carnival event held in Singapore. He has some tickets in his pocket, and these tickets can be used at the game booths in the carnival. Suppose there are nn colors of tickets. Each ticket is painted in exactly one color and has a non-negative integer printed on it. Different tickets may have the same number. According to the carnival rules, nn is guaranteed to be even.

Ringo has mm tickets of each color, so he has a total of nmn \cdot m tickets. For the jj-th ticket of color ii, the printed number is x[i][j]x[i][j] (0in10 \le i \le n-1 and 0jm10 \le j \le m-1).

A ticket game consists of kk rounds, numbered from 00 to k1k-1. Each round works as follows:

  • First, Ringo selects one ticket from each color, forming a set of nn tickets.
  • Then, the game host writes down the numbers on the tickets in this set: a[0],a[1],,a[n1]a[0],a[1],\ldots,a[n-1]. The order of these nn integers does not matter.
  • Next, the host draws a special card from a lucky draw box, which has an integer bb printed on it.
  • For each ticket number a[i](0in1)a[i](0\le i \le n-1) in the set, the host computes the absolute value of the difference between a[i]a[i] and bb. Let SS be the sum of these nn absolute differences.
  • The value SS is the reward Ringo gets in this round.
  • After the round ends, all tickets in the set are discarded and will not be used in future rounds.

After the kk rounds are finished, Ringo discards all remaining tickets in his pocket.

After careful observation, Ringo discovers that the ticket game is rigged. In fact, there is a printer inside the lucky draw box. In each round, the host first finds an integer bb that minimizes the reward of the current round, and then prints that number on the special card that he draws.

Knowing this, Ringo wants to design the ticket allocation plan for each round so that the total reward over the kk rounds is maximized.

Implementation details

You need to implement the following function:

long long find_maximum(int k,std::vector<std::vector<int>> x)
  • kk: the number of rounds.
  • xx: an n×mn \times m array recording the numbers on the tickets. For each color, the tickets are sorted in non-decreasing order of the numbers above.
  • This function will be called only once.
  • This function should call the function allocate_tickets exactly once (see below) to describe the allocation plan of tickets across the kk rounds, where each round corresponds to one set of tickets. The allocation plan should maximize the total reward.
  • This function must return the maximum possible total reward.

The function allocate_tickets is defined as follows:

void allocate_tickets(std::vector<std::vector<int>> s)
  • ss: an n×mn \times m array. If the jj-th ticket of color ii is assigned to round rr, then s[i][j]s[i][j] should be rr; if it is not used, it should be 1-1.
  • For each 0in10 \le i \le n-1, in s[i][0],s[i][1],,s[i][m1]s[i][0],s[i][1],\ldots,s[i][m-1], each value 0,1,,k10,1,\ldots,k-1 must appear exactly once, and all other elements should be 1-1.
  • If there are multiple optimal allocation plans that achieve the maximum reward, you may output any one of them.

Hint

Sample explanation

Example 1

Consider the following function call:

find_maximum(2, [[0, 2, 5],[1, 1, 3]])

This means:

  • The game is played for k=2k=2 rounds.
  • The integers on the tickets of color 00 are 0,20,2 and 55.
  • The integers on the tickets of color 11 are 1,11,1 and 33.

One optimal allocation plan is:

  • In round 00, Ringo chooses the 00-th ticket of color 00 (printed with 00) and the 22-nd ticket of color 11 (printed with 33). The minimum reward in this round is 33. For example, the host can choose b=1b=1: 10+13=1+2=3|1-0| + |1-3| = 1+2 = 3.
  • In round 11, Ringo chooses the 22-nd ticket of color 00 (printed with 55) and the 11-st ticket of color 11 (printed with 11). The minimum reward in this round is 44. For example, the host can choose b=3b=3: 31+35=2+2=4|3-1|+|3-5|=2+2=4.
  • Therefore, the sum of rewards over the two rounds is 3+4=73+4=7.

To output this allocation plan, find_maximum should call allocate_tickets as follows:

allocate_tickets([[0, -1, 1], [-1, 1, 0]])

Finally, find_maximum should return 77.

Example 2

Consider the following function call:

find_maximum(1, [[5, 9], [1, 4], [3, 6], [2, 7]])

This means:

  • The game is played for only one round.
  • The numbers on tickets of color 00 are 55 and 99.
  • The numbers on tickets of color 11 are 11 and 44.
  • The numbers on tickets of color 22 are 33 and 66.
  • The numbers on tickets of color 33 are 22 and 77.

One optimal allocation plan is:

  • In round 00, Ringo chooses the 11-st ticket of color 00 (printed with 99), the 00-th ticket of color 11 (printed with 11), the 00-th ticket of color 22 (printed with 33), and the 11-st ticket of color 33 (printed with 77). The minimum reward in this round is 1212. For example, the host can choose b=3b=3: 39+31+33+37=6+2+0+4=12|3-9| + |3-1| + |3-3| + |3-7| = 6 + 2 + 0 + 4 = 12.

To output this allocation plan, find_maximum should call allocate_tickets as follows:

allocate_tickets([[-1, 0], [0, -1], [0, -1], [-1, 0]])

Finally, find_maximum should return 1212.

Constraints

  • 2n15002\le n\le 1500 and nn is even.
  • 1km15001\le k\le m\le 1500.
  • 0x[i][j]1090 \le x[i][j] \le 10^9 (for all 0in10 \le i \le n-1 and 0jm10 \le j \le m-1).
  • x[i][j1]x[i][j]x[i][j-1] \le x[i][j] (for all 0in10 \le i \le n-1 and 0jm10 \le j \le m-1).

Subtasks

  1. (11 points) m=1m=1.
  2. (16 points) k=1k=1.
  3. (14 points) 0x[i][j]10 \le x[i][j] \le 1 (for all 0in10 \le i \le n-1 and 0jm10 \le j \le m-1).
  4. (14 points) k=mk=m.
  5. (12 points) n,m80n,m \le 80.
  6. (23 points) n,m300n,m \le 300.
  7. (10 points) No additional constraints.

Sample grader program

The sample grader reads the input in the following format:

Line 11: n m kn\ m\ k
Line 2+i2+i (0in10 \le i \le n-1): x[i][0] x[i][1]  x[i][m1]x[i][0]\ x[i][1]\ \ldots \ x[i][m-1]

The sample grader prints your output in the following format:

Line 11: the return value of find_maximum
Line 2+i2+i (0in10 \le i \le n-1): s[i][0] s[i][1]  s[i][m1]s[i][0]\ s[i][1]\ \ldots\ s[i][m-1]

Translated by ChatGPT 5