参考代码:
#include <cstdio> #define mid ((l + r) / 2) #include <algorithm> using namespace std; const int maxn = 200000 + 5, maxvalue = 1e9; int n, value[maxn]; int main() { scanf("%d", &n); for(int i = 0; i < n; i ++) { scanf("%d", &value[i]); if(value[i] < 0) value[i] = - value[i]; } sort(value, value + n); long long ans = 0; int l, r; for(int i = 0; i < n; i ++) { l = i + 1, r = n - 1; while(l <= r) { if(2 * value[i] >= value[mid]) l = mid + 1; else r = mid - 1; } ans += (l - i - 1); } printf("%lld ", ans); return 0; }