4 solutions
-
1
#include <iostream> #include <algorithm> using namespace std; // 1 1 2 3 C = 1 int erfen2(int* arr,long long key,int N) { int l = -1; int r = N; while (l + 1 < r) { int mid = l + ((r - l) >> 1); if (l == -1) { mid = (l + r) >> 1; } if (arr[mid] <= key) { l = mid; } else { r = mid; } } return r;//大于 key 的 第一个值,r - 1 是 key 的右边界 } int erfen2B(int* arr, long long key, int N) { int l = -1, r = N; while (l + 1 < r) { int mid = l + ((r - l) >> 1); if (l == -1) { mid = (l + r) >> 1; } if (arr[mid] >= key) { r = mid; } else { l = mid; } } return l;//小于 key 的 第一个值, l + 1 是 key 的 左边界。 } int main(void) { int N, C; cin >> N >> C; int* arr = new int[N + 5]; for (int i = 0; i < N; ++i) { cin >> arr[i]; } sort(arr, arr + N); int ans = 0; for (int i = 0; i < N; ++i) { long long temp = arr[i] + C;// 我们只要找到这个值就行了。 ans += erfen2(arr, temp, N) -1 - erfen2B(arr, temp, N); } cout << ans << endl; return 0; }
Information
- ID
- 279
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 297
- Accepted
- 57
- Uploaded By