TITLE: Complexity of a weirdo two-dimensional sorting problem
QUESTION [13 upvotes]: Please forgive me if this is easy for some reason.
Suppose given $S$, a set of $n^2$ points in $\mathbb{R}^2$.
I want to choose a bijective map $f$ from $S$ to the set of lattice points in $\lbrace 0,\ldots,n-1\rbrace \times \lbrace 0,\ldots,n-1\rbrace$ so as to maximize the sum, over all $p$ in $S$, of the dot product $p \cdot f(p)$.
If, instead of $\mathbb{R}^2$, I had $\mathbb{R}^1$, and I was putting $S$ in bijection with $\lbrace0,\ldots,n-1\rbrace$, then this would simply be sorting $S$ and one knows how to do that fast.
For this problem, it's not even obvious to me how to do it in a number of steps that's polynomial in $n$.  
Is this easy?  Is it an example of a known genre of optimization problem?

REPLY [2 votes]: Here are some more cross-connections: The problem can be massaged into a least-squares matching problem
$$\sum_{p\in S} \|(-f(p))-p\|^2 \to \min$$ between $S$ and the grid rotated by $180^\circ$ (which in this case happens to be the same as the original grid, after translation), and hence it becomes a problem of optimal mass transport. The classical reference is:

Franz Aurenhammer, Friedrich Hoffmann, and Boris Aronov,
  Minkowski-type theorems and least-squares clustering. Algorithmica 20:
  61–76 (1998). DOI: 10.1007/PL00009187

There is an implementation in R: https://rdrr.io/cran/transport/man/aha.html
See also my little note
Two applications of point matching. I would also be interested to find a provably faster algorithm than the general matching procedures, which would make use of the geometric situation. (Fast practical algorithms and fast approximation algorithms are mentioned in the references of these papers and the software.)