fork download
#include <bits/stdc++.h>
using namespace std;

int upperbound(vector<int> &a, int x)
{
	int l = 0, r = a.size() - 1;
	while(l <= r)
	{
		int mid = (l + r)/2;
		if(a[mid] <= x)
			l = mid;
		else
		{
			if(mid == 0)
				return 0;
			else
			{
				if(a[mid - 1] <= x)
					return mid;
				else
					r = mid;
			}
		}
	}
	return a.size();
}

int lowerbound(vector<int> &a, int x)
{
	int l = 0, r = a.size() - 1;
	while(l <= r)
	{
		int mid = (l + r)/2;
		if(a[mid] >= x)
			r = mid;
		else
		{
			if(mid == a.size())
				return a.size();
			else
			{
				if(a[mid + 1] >= x)
					return mid;
				else
					l = mid;
			}
		}
	}
	return 0;
}

int main() 
{
	int n;  cin>>n;
	vector<int> ip;
	for(int i=0; i<n; i++) 
	{
		int temp;  cin>>temp;
		ip.push_back(temp);
	}
	int x;  cin>>x;
	// cout << lowerbound(ip,x) << " " << upperbound(ip,x) << endl;
	// cout << (first(ip,x)+last(ip,x))/2 << endl;
	cout << (lowerbound(ip,x)+upperbound(ip,x))/2 << endl;
	return 0;
}
Success #stdin #stdout 0.01s 5288KB
stdin
10
1 2 2 2 2 3 4 5 6 7
2
stdout
2