其他分享
首页 > 其他分享> > [AGC034D] Manhattan Max Matching

[AGC034D] Manhattan Max Matching

作者:互联网

https://atcoder.jp/contests/agc034/tasks/agc034_d

Time Limit: 5 sec / Memory Limit: 1024 MB

Score : \(1200\) points

Problem Statement

Snuke is playing with red and blue balls, placing them on a two-dimensional plane.

First, he performed \(N\) operations to place red balls. In the \(i\)-th of these operations, he placed \(RC_i\) red balls at coordinates \((RX_i,RY_i)\). Then, he performed another \(N\) operations to place blue balls. In the \(i\)-th of these operations, he placed \(BC_i\) blue balls at coordinates \((BX_i,BY_i)\). The total number of red balls placed and the total number of blue balls placed are equal, that is, \(\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i\). Let this value be \(S\).

Snuke will now form \(S\) pairs of red and blue balls so that every ball belongs to exactly one pair. Let us define the score of a pair of a red ball at coordinates \((rx, ry)\) and a blue ball at coordinates \((bx, by)\) as \(|rx-bx| + |ry-by|\).

Snuke wants to maximize the sum of the scores of the pairs. Help him by finding the maximum possible sum of the scores of the pairs.

Constraints

解答

明显用网络流……但是图太大了!而且曼哈顿距离暴难处理!!

……然后想到拆点:\((x_1, y_1), (x_2, y_2)\)之间的曼哈顿距离为

\[\begin{aligned} dist &= |x_1-x_2|+|y_1-y_2| \\ &= \max\{x_1-x_2, x_2-x_1\}+\max\{y_1-y_2, y_2-y_1\} \\ &= \max\{(x_1+y_1)+(-x_2-y_2), \cdots, (-x_1-y_1)+(x_2+y_2)\} \end{aligned} \]

之后,就只有在整体上取\(\max\),随便建图就可以了。

Editorial

https://img.atcoder.jp/agc034/editorial.pdf

Bonus: This problem can also be solved in \(O(S \log N)\) time.https://codeforces.com/blog/entry/67345?#comment-515285

Without affecting the score, we can assume that instead of getting the score of |rx − bx| + |ry − by| for a pair, we can choose one of the following scores for a pair:

For each ball, let us decide in advance which of these four to use.

That is, we will classify the red balls into the following four types:

We will also classify the blue balls into the following four types:

Then, we can form the pairs if the number of red balls and that of blue balls are equal for each type.

We want to find the maximum score of such a classification.

We can solve it as a minimum-cost flow problem. Let us build a graph with 1 (source) +N (operations with red balls) +4 (types) +N (operations with blue balls) +1 (sink) vertices and the following edges:

Then send the flow of S (the number of balls). We can resolve negative costs by adding some offset to
each edge.

There are O(N) edges, and the amount of flow is O(S), so the time complexity is O(SN log N).

标签:blue,balls,Max,AGC034D,rx,ry,score,bx,Manhattan
来源: https://www.cnblogs.com/frank3215/p/agc034d.html