【CCF】——Intermediate number (detailed analysis)

Problem description
  In an integer sequence a1, a2, …, an, if there is a certain number, the number of integers greater than it is equal to the number of integers less than it, then it is called an intermediate number. In a sequence, there may be multiple intermediate numbers with different subscripts, and the values ​​of these intermediate numbers are the same.
  Given a sequence of integers, find the value of the middle number of the sequence of integers.
Input format
  The first line of input contains an integer n, which represents the number of numbers in the integer sequence.
  The second line contains n positive integers, representing a1, a2, …, an in turn.
Output format
  If the middle number of the agreed sequence exists, the value of the middle number is output, otherwise, -1 is output to indicate that there is no middle number.
Sample input

6
2 6 5 6 3 5

Sample output

5

Example explanation
  There are 2 numbers smaller than 5, and there are 2 numbers larger than 5.
Sample input

4
3 4 6 7

Sample output

-1

Sample explanation
  4 numbers in the sequence do not satisfy the definition of the middle number.
Sample input

5
3 4 6 6 7

Sample output

-1

Example explanation
  all 5 numbers in the sequence do not satisfy the definition of the middle number.
Evaluation use case scale and conventions
  For all evaluation use cases, 1 ≤ n ≤ 1000, 1 ≤ ai ≤ 1000.

I submitted it again, and there was no compilation error before submission, O(∩_∩)O haha~.
When you see the middle number, it is generally related to the parity, because the middle number of the parity is not necessarily the same, and the even number is more troublesome, but this question is obviously not necessary to divide the parity, first give an even number example, the 2 in the sample 6 5 6 3 5, sorted from small to large, it is 2 3 5 5 6 6. The subscripts 2 and 3 are the middle numbers of the combination, first look at n/2=3, which is 5, look to the left , The qualified ones are 2 3, len1=2, look to the right, the qualified ones are 6 6, len2=2, satisfy. If it is n/2-1, that is, 5, look to the left, if the condition is 2 3, len1=2, and look to the right, if the condition is 6 6, len2=2, it also meets, because the same are filtered It is dropped, so the even number can be directly n/2, which can also be consistent with the middle number subscript of the odd number, reducing the amount of code. To give another example of odd numbers, the 3 4 6 6 7 in the sample is 3 4 6 6 7 sorted from smallest to largest, and the middle number is n/2=2, that is, a[2]=6, look to the left. The qualified ones are 3 4, len1=2, and look to the right. The qualified ones are 7, len2=1, which is not met, so output -1. In short, when counting to the left and right, only those that are not equal to the mid value are counted
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    
    
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	int a[n];
	for(int i = 0;i<n;i++){
    
    
		cin >> a[i];
	}
	sort(a,a+n);
	int len1 = 0,len2 = 0;
	int mid_index = n/2,mid = a[n/2];
	//只有一个数的时候,左右相等,一定符合,直接输出,
	//这样可以防止后边循环中的下标越界错误,因为1/2=0,而0-1=-1,下标越界
	if(n==1){
    
    
		cout << a[0];
		return 0;
	}
	for(int i = mid_index-1;i>=0;i--){
    
    
		if(a[i]!=mid)
			len1++;
	}
	for(int i = mid_index+1;i<n;i++){
    
    
		if(a[i]!=mid)
			len2++;
	}
	if(len1==len2)
		cout << mid;
	else
		cout << -1;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/108549464