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
|
#include <stdio.h>
#include <stdbool.h>
int main (void) {
int n, k, s = 0;
scanf("%d %d", &n, &k);
int a[n];
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
int d, l = n-1;
for (int i = 0; i < n-1; i++)
if (2*a[i] <= k && 2*a[i+1] > k)
l = i;
d = l;
fprintf(stderr, "l je %d, d je %d\n", l, d);
do {
if (a[l]+a[d] == k) {
int lc = l;
int dc = d;
int add = 0;
while (lc >= 0 && a[lc] == a[l])
lc--;
while (dc <= n-1 && a[dc] == a[d])
dc++;
if (a[l] == a[d]) // (dc-lc) choose 2
add = ((dc-lc-1)*n)/2;
else
add = (d-dc+1)*(lc-l+1);
fprintf(stderr, "l=%d, d=%d, a[l]=%d, a[d]=%d, lc=%d, dc=%d, add=%d\n", l, d, a[l], a[d], lc, dc, add);
l = lc;
d = dc;
s += add;
}
if (l-1 >= 0)
l--;
if (d+1 < n)
d++;
} while (d < n-1 || l > 0);
printf("%d\n", s);
}
|