84分求助,#2#3 TLE
  • 板块P1102 A-B 数对
  • 楼主zhibuba
  • 当前回复5
  • 已保存回复5
  • 发布时间2020/5/9 10:06
  • 上次更新2023/11/7 02:49:53
查看原帖
84分求助,#2#3 TLE
319478
zhibuba楼主2020/5/9 10:06
#include <stdio.h>
#include <stdlib.h>

int cmp(const void * a, const void * b)
{
	return	*((long long *) a) - *((long long *) b);
}

int count(const long long * P, const long long A[], const int N)
{
	int n = 1;
	long long val = *P;
	long long * p = P;
	while (--p >= A && *p == val)
		n++;
	p = P;
	while (++p < A + N && *p == val)
		n++;
	return n;
}

int main(void)
{
	int N, C;
	scanf("%d %d", &N, &C);
	long long num[200000];
	for (int i = 0; i < N; i++)
		scanf("%lld", &num[i]);
	qsort(num, N, sizeof(long long), cmp);
	int ans = 0;
	for (int i = 0; i < N; i++)
	{
		long long A = num[i] + C;
		long long * p = bsearch(&A, num, N, sizeof(long long), cmp);
		if (p)
			ans += count(p, num, N);
	}
	printf("%d", ans);
	return 0;
}
2020/5/9 10:06
加载中...