1 条题解
-
0
解题思路
将不等式 改写为 。这看起来简单多了。
我们构造数组 ,其中 ,然后对 进行排序。现在问题转化为:找到满足 且 的无序对 的数量。
我们从左到右遍历 中的元素。为了简化,我们只考虑“较大的”加数。因为和 必须成立,所以其中至少有一个加数是正数。因此,如果 ,直接跳过。
现在 ,我们需要计算所有满足 且 的 的数量。这意味着所有满足 的 ()都是可行的。我们可以使用
std::lower_bound或二分查找找到满足条件的最左边的位置 ,然后将 加入答案,接着处理下一个元素。时间复杂度:。
#include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<int> a(n), b(n); for (auto &it : a) cin >> it; for (auto &it : b) cin >> it; vector<int> c(n); for (int i = 0; i < n; ++i) { c[i] = a[i] - b[i]; } sort(c.begin(), c.end()); long long ans = 0; for (int i = 0; i < n; ++i) { if (c[i] <= 0) continue; int pos = lower_bound(c.begin(), c.end(), -c[i] + 1) - c.begin(); ans += i - pos; } cout << ans << endl; return 0; }
- 1
信息
- ID
- 6884
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者