Week4: TT mysterious gift - dichotomy seeking median

Title Contents
TT is a severe cat lovers, cat channel on the daily indulge in B station.

One day, a friend ZJM decided to TT TT a problem, if TT can solve this problem, ZJM will buy a cute cat gave TT.

SUMMARY is given array cat [i] a number N, and generate a new array ans [i] with the array. It is defined as the new array for any i, j and i = j, are ans [] = abs (cat [i] - cat [j])!, 1 <= i <j <= N. This new array. Determine the median, then the median is the sort (len + 1) / 2 corresponding to the position number, '/' is a rounding.

TT desperately wanting bird cute cat, can you help him?

Input format
multiple sets of inputs, each input one N, the number N expressed, after the input of a sequence of length N cat, cat [i] <= 1e9, 3 <= n <= 1e5.

Output format
output ans new array of the median.

Input Example

4
1 3 2 4
3
1 10 2

Output Example

1
8

Problem-solving ideas
Obviously if I TT, then I will give up the cat.
Hey, median Well, if we can narrow the scope of n point that this question will be able to direct violence algorithm lore, but unfortunately not change.

So how half of it?
For a string of numbers, in our case the number n of the string of numbers is known, can easily be determined as the median location of (n + 1) >> 1.

So if we look at the opposite of what?
Obviously we do not even work out ans array directly, you can predict in advance where to position the median where it should be ((n * (n - 1 )) / 2 + 1) / 2, where n is an array of cat the size of.

So we can continue to analyze:
the known position of the median, we only need to be half ans:
then for a certain number of P ans,
only it requires a less number of the number of the line, this If the number is equal to the number of median supposed to have, it shows that P is the median.
Of course, if P is less than the median, or more, so the two points on the line.

But since used the half, then we need the necessary conditions: the dichotomy of the array should be monotonic.
Look at the title: ans [] = abs (cat [i] - cat [j])
How can we put an absolute value out of it?
Apparently only need to ensure that cat [i] is greater than the cat [j] on it.
We can arrange in advance for cat, and then ensure that i is greater than j on the line.
Thus in this case the formula becomes: P> = cat [i] -cat [j], where i> j.
The cat [j] moved to the left of the formula: P + cat [j]> = cat [i].
For a binary P out, we only need to j is fixed, and two points on the line i, so that the number can be determined for all i and j satisfy this equation, i.e. ans array equal to the number P is smaller than the number of .
Complexity is O (logk + nlogn), is acceptable.

Then there is the code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>

using namespace std;

vector<int>cat;
int n;

bool cmp(const int& a, const int& b)
{
	return a < b;
}

int Fun(int &mid,const int &rank)
{
	int currank = 0;
	for (int i = 0; i < n; i++)
	{
		int temp = cat[i] + mid;

		int l = 0, r = n - 1, ans = -1;
		while (l <= r)
		{
			int mid2 = (l + r) >> 1;
			if (cat[mid2] <= temp)//符合cat[j]+cat[i]<=P
			{
				ans = mid2;
				l = mid2 + 1;
			}
			else r = mid2 - 1;
		}

		if (ans != -1) currank += ans - i;
	}

	if (currank < rank) return 1;//小于
	if (currank >= rank) return 2;//大于等于
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> n)
	{
		cat.clear();
		
		for (int i = 0; i < n; i++)
		{
			int temp;
			cin >> temp;
			cat.push_back(temp);
		}

		sort(cat.begin(), cat.end(), cmp);

		int rank = ((n * (n - 1)) / 2 + 1) / 2;//中位数的名次

		int l = 0, r = cat.back() - cat.front(),ans=-1;

		while (l <= r)
		{
			int mid = (l + r) >> 1;
			int opnum = Fun(mid, rank);
			if (opnum == 2)
			{
				ans = mid;
				r = mid - 1;
			}
			else if (opnum == 1)
			{
				l = mid + 1;
			}

		}

		if (ans != -1) cout << ans << endl;
	}
}

If you use iostream, apparently do not forget to use

ios::sync_with_stdio(false);
cin.tie(0);

Bloody lessons
Here Insert Picture Description

Published 21 original articles · won praise 3 · Views 414

Guess you like

Origin blog.csdn.net/qq_44506233/article/details/104980129
TT