swustoj Soldiers

In a gridded playground, n soldiers stand scattered on the grid points. Grid points are represented by integer coordinates (x,y). Soldiers can move one step up, down, left, and right along the edge of the grid, but only one soldier can be at any grid point at a time. According to the order of the officer, the soldiers should be neatly arranged in a horizontal queue, that is, arranged in (x, y), (x+1, y), ..., (x+n-1, y). How to choose the values ​​of x and y so that the soldiers line up with the least total number of moves. Calculate the minimum number of moves required to line up all soldiers.

enter

Row 1 is the number of wells n, 1<=n<=10000. The next n lines are the positions of the wells, each with 2 integers x and y, -10000<=x, y<=10000.

output

The number in row 1 is the minimum number of steps a soldier needs to move in a line.

sample input

5
1 2
2 2
1 3
3 -2
3 3

Sample output

8

Through proper movement sequence and movement route, there will be no two soldiers standing at the same point at the same time.
The second question requires the best way to move (that is, the minimum number of steps to move). The
question requirements can be transformed into the "final position" for soldiers to stand. , that is, how to take the "final position" so that the number of steps the soldiers move is the least (optimal)
Considering the direction of the Y-axis
Let the target coordinate be M, that is, the coordinate value of the Y-axis that n soldiers need to move to in the end is M
n soldiers The Y-axis coordinates of are:
Y0, Y1, Y2 …… …… Yn-1
, the optimal number of steps S=|Y0-M|+|Y1-M|+|Y2-M|+ …… …… +| Yn-1-M|
Conclusion: M takes the value of the middle point so that S is the least (optimal)
Proof: ...
recursively starting from the top and bottom two soldiers ... the
best position The
best position
comes down to the end, The Y-axis coordinate value of the soldier in the middle position is the Y-axis coordinate of the "final position".
There may be two cases.
The total number of soldiers is an even number
. Location
solution: sort all Y-axis coordinates (O(nlogn)) or perform linear time selection (O(n))
and then take the Y-axis coordinate value of the "middle" point as the value of the best position M
Finally pass The formula calculates the optimal number of steps to move in the Y-axis direction.
Considering the X-axis direction, it is
first necessary to sort the X-axis coordinate values ​​of all soldiers,
and then move to the corresponding position of each soldier in order from left to right. Final position" (optimal), the sum of the steps moved is the number of steps that need to be moved in the X-axis direction
For example, if the leftmost soldier moves to the leftmost position of the "final position", and the second soldier moves to the second position of the "final position",
the total number of steps is: the number of steps moved by the first soldier + the number of steps moved by the second soldier + ... + Soldier n moving steps
How to determine the best "final position" in the X-axis direction?
There are a total of n soldiers
and their corresponding X-axis coordinates are: X0, X1, X2 …… …… Xn-1 is assumed
, the X-axis coordinates of the "final position" that the soldiers need to move to are: k, k+1, k+ 2 …… …… k+(n-1)
, then the optimal number of steps S=|X0-k|+|X1- (k+1) |+|X2-(k+2)|+ …… +| Xn-1-(k+(n-1))|
After deformation S=|X0-k|+|(X1-1)-k|+|(X2-2)-k|+ ……  +|( Xn-1-(n-1))-k|
Note that the form of the formula is the same as the consideration in the Y-axis direction. It is also the absolute value of n known numbers minus an undetermined number, and then summed.
Therefore, it is still The k value is obtained by taking the median method, and finally the optimal solution is calculated

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;

intmain()
{
	int n;
	while (cin >> n) {
		int x[10005];
		int y[10005];
		for (int i = 0;i < n;i++) {
			scanf("%d%d", x + i, y + i);
		}
		sort(x, x + n);
		sort(y, y + n);
		for (int i = 0;i < n;i++) {
			x[i] -= i;
		}
		sort(x, x + n);
		int midx = x[(n-1) / 2];
		int midy = y[(n-1) / 2];
		int ans = 0;
		for (int i = 0;i < n;i++) {
			ans += abs(x[i] - midx);
			ans += abs(y[i] - midy);
		}
		cout << ans << endl;
	}
	
	return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325668715&siteId=291194637
Recommended