about summary refs log tree commit diff
path: root/NTU/keba2.c
blob: d9316fd2c2c3abba592fbf5727d6cb8e7505041a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdlib.h>
#include <stdio.h>

int cmp(const void *x, const void *y)
{
	return *(long long *) x - *(long long *) y;
}

int main()
{
	short n, i;
	long b;
	scanf("%hd %ld", &n, &b);
	long long *s = malloc(n * 8);
	for (i = 0; i < n; i++)
		scanf("%lld", &s[i]);

	qsort(s, n, 8, cmp);

	short m = 0, hist[10000] = {1};
	long long a[10000];
	a[0] = s[0];
	for (i = 1; i < n; i++) {
		if (s[i] != a[m]) {
			m++;
			a[m] = s[i];
		}
		hist[m]++;
	}
	m++;

	long val = 0;
	long long foo;
	void *p;
	for (i = 0; i < m; i++) {
		foo = b - a[i];
		if (foo == a[i])
			val += hist[i] * (hist[i] - 1) / 2;
		if (foo <= a[i])
			break;
		p = bsearch(&foo, a, m, 8, cmp);
		if (p != NULL)
			val += hist[i] * hist[(long long *) p - a];
	}

	printf("%ld\n", val);

	return 0;
}