[Programming thinking and practice Week4 job C] TT mystery gift

Subject description:

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.

Input:

Plural 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.

Sample Inpout:

4
1 3 2 4
3
1 10 2

Output:

Output new array ans median

Sample Output:

1
8

Ideas:

For cat input array, we can know the size of the array ans, so we can know the median rank, i.e. the number smaller than the number of bits, a given number P, if it is less than the median rank number, he is less than the median, if the rank is greater than the median, which is larger than the median. We can see that the answer is monotone and therefore can be considered half of practice. The cat we sort the array, the array may be ans i <j, cat [j] -cat [i] is obtained, to achieve the purpose of the absolute value, we can approximate the median range can be determined, i.e. the last array Save the first number is a number which is a maximum value, must be greater than zero and, accordingly, we can median range of half and half in accordance with the number obtained, which then determines the median size is greater than if the median, half of the first half, or half of the second half.
At this time, the remaining question is how the given binary data P obtained with a median size of his determination, i.e., determines the number smaller than the number of his because ans [x] <P, i.e. cat [j] -cat [i] <p (i < j) i.e. cat [i] + P> cat [j], whereby we traverse i, j is obtained a range less than the number of data obtained is referred to as P COUNT, we obtaining data equal to P is also referred to as the number of temp.
The median rank referred to target, if count <target && count + temp> = target then P is the median, otherwise, if the count + temp <target is less than the median P, to continue the range of the second half of binary digits, and then P is greater than the median, two continuing half minutes ago. Until the median.

Code:

#include <iostream>
#include<algorithm>
using namespace std; 
const int size=1e5+10;
int number[size];
int end(int x,int left,int n)
{//小于x的最后一个数 
	int ans=-1;
	int ll=left+1,rr=n-1;
	while(ll<=rr)
	{
		int mid=(ll+rr)>>1;
		if(number[mid]<x)
		{
			ans=mid;
			ll=mid+1;
		}
		else
			rr=mid-1;
	}
	return ans;
}
int getans(int n)
{
	int right=number[n-1]-number[0];
	int left=0;
	int sum=0;
	for(int i=1;i<n;i++)
		sum+=i;
	int target=(sum+1)/2;
	while(left<=right)
	{
		int mid=(left+right)>>1;
		int count=0,temp=0,ans=0;
		for(int i=0;i<n;i++)
		{
			int en=end((mid+number[i]),i,n);
			if(en!=-1)
			{
				count+=(en-i);	
				for(int j=en+1;j<n&&number[j]==(number[i]+mid);j++)
					temp++;			
			}
			else
				for(int j=i+1;j<n&&number[j]==(number[i]+mid);j++)
					temp++;	
		}
		if(count<target&&(count+temp)>=target)
			return mid;
		else if(count+temp<target)
			left=mid+1;
		else
			right=mid-1;	
	}
}
int main(int argc, char** argv) {
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%d",&number[i]);
		sort(number,number+n);
		int ans=getans(n);
		printf("%d\n",ans);
	}
	return 0;
}
Published 24 original articles · won praise 8 · views 531

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/104875013