#include <cstdio>
#include <algorithm>
typedef long long ll;
const int maxn = 2e5 + 5;
int a[maxn];
int main () {
    int T, n;
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf ("%d", &a[i]);
        }
        std::sort (a, a + n);
        ll p = 0, ans = 0;
        for (int i = 0; i < n; i++) {
            while (a[p + 1] - a[i] <= 2 && p + 1 < n) {
                p++;
            }
            if (p - i >= 2) {
                ans = ans + (p - i) * (p - i - 1) / 2;
            }
        }
        printf ("%lld\n", ans);
    }
    return 0;
}
E1. Close Tuples (easy version) - CF1462E1
原文:https://www.cnblogs.com/chantmee/p/14167238.html