gpt4 book ai didi

algorithm - 公平划分王国

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:16:20 24 4
gpt4 key购买 nike

<分区>

最近,我参加了编程比赛。它有一个问题,我还在考虑。编程语言并不重要,但我已经用 C++ 编写了它。任务是这样的:

As you already know, Flatland is located on the plane. There are n cities in Flatland, i-th of these cities is located at the point (xi, yi). There are ai citizens living in i-th city. The king of Flatland has decided to divide the kingdom between his two sons. He wants to build a wall in the form of infinite straight line; each of the parts will be ruled by one of the sons. The wall cannot pass through any city. To avoid envy between brothers, the populations of two parts must be as close as possible; formally, if a and b are the total number of citizens living in cities of the first and the second part respectively, the value of |a - b| must be minimized. Help the king to find the optimal division. Number of cities is less than 1000. And all coordinates are integers. Output of algorithm should be integer number of minimal |a-b|

好吧,如果我知道线的方向,这将是一件很容易的事 - 二分查找:Black numbers are population of cities, and red is total population in that part

我不想要代码,我想要想法,因为我没有。如果我捕获了想法,我就可以编写代码了!

我不知道最佳方向,但我认为可以通过某种方式找到它。那么是否可以找到它或者是否可以通过其他方式解决此任务?

水平/垂直线不是最优的示例:

1

\
\
2 \ 1

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com