#2#3TLE求助(用了二分)
查看原帖
#2#3TLE求助(用了二分)
1053560
Kotori_Kawaii楼主2023/10/30 22:00
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<math.h>
using namespace std;
int a[200500];
int qceil(double a) {
	int s = (int)a;
	if (a - s > 1e-6) {
		s++;
	}
	return s;
}

int main()
{
	int n, c,k,cnt=0;
	scanf("%d%d", &n, &c);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	int left = 0, right = n - 1;
	sort(a, a + n);
	for (int i = n - 1; i >= 0; i--) {
		k = a[i] - c;
		left = 0, right = i;
		while (left < right) {
			if (k <= a[(left + right) / 2])
				right = (left + right) / 2;
			else
				if (right - left != 1) {
					left = (left + right) / 2;
				}
				else {
					left = qceil((left * 1.0 + right) / 2);
				}
		}
		if (a[left] - k==0) {
			cnt++;
			left--;
			right++;
			while (a[left] - k==0) {
				left--;
				cnt++;
			}
			while (a[right] - k == 0) {
				right++;
				cnt++;
			}
		}
	}
	printf("%d", cnt);
	return 0;
}
2023/10/30 22:00
加载中...