ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[AGC034D] Manhattan Max Matching

2021-06-22 08:01:41  阅读:260  来源: 互联网

标签:blue balls Max AGC034D rx ry score bx Manhattan


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

  • \(1 \leq N \leq 1000\)
  • \(0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9\)
  • \(1 \leq RC_i,BC_i \leq 10\)
  • \(\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i\)
  • All values in input are integers.

解答

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

……然后想到拆点:\((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:

  • (rx − bx) + (ry − by) = (rx + ry) + (−bx − by)
  • −(rx − bx) + (ry − by) = (−rx + ry) + (bx − by)
  • (rx − bx) − (ry − by) = (rx − ry) + (−bx + by)
  • −(rx − bx) − (ry − by) = (−rx − ry) + (bx + by)

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:

  • Type 0: adds (rx + ry) to the score.
  • Type 1: adds (−rx + ry) to the score.
  • Type 2: adds (rx − ry) to the score.
  • Type 3: adds (−rx − ry) to the score.

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

  • Type 0: adds (−bx − by) to the score.
  • Type 1: adds (bx − by) to the score.
  • Type 2: adds (−bx + by) to the score.
  • Type 3: adds (bx + by) to the score.

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:

  • from the source to each “operations with red balls” vertex: an edge of capacity RCi and cost 0
  • from each “operations with red balls” vertex to each “type” vertex: an edge of capacity ∞ and cost −(the score above)
  • from each “type” vertex to each “operations with blue balls” vertex: an edge of capacity ∞ and cost −(the score above)
  • from each “operations with blue balls” vertex to the sink: an edge of capacity BCi and cost 0

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有