排序之后,能与牛i + 1匹配的,一定能与牛i匹配,所以我们弄一个左指针l,从右向左看每一头牛,每次移动左指针,使左指针左面的都是满足的,右面的都是不满足的即可。
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 20002; int main() { //freopen("D:\\t.txt", "r", stdin); int n, s; int cow[maxn]; scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) scanf("%d", &cow[i]); sort(cow, cow + n); int l, ans = 0; l = 0; for (int i = n - 1; i >= 0; i--) { while (cow[l] + cow[i] <= s && l < n) l++; ans += l; if (l - 1 >= i) ans--; } printf("%d\n", ans / 2); return 0; }