题解:P11951 [科大国创杯初中组 2023] 数数
题目简述
给定长度为
主要思路
首先可以排除
考虑二分,给定的初数组不一定有序,所以需要先排一遍序。随后用 lower_bound 分别枚举第一个满足
时间复杂度
注意事项
在最极端情况下,答案会达到
AC Code
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double db;
const int N = 8e3 + 10;
const int INT_INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
// ----------------------------
// ----------------------------
int a[N];
// ----------------------------
int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
// ----------------------------
ll ans = 0;
sort(a + 1, a + n + 1);
for (int i = 1; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
ans += (lower_bound(a + j + 1, a + n + 1, a[i] + a[j]) - a) - (lower_bound(a + j + 1, a + n + 1, a[j]) - a);
}
}
// ----------------------------
cout << ans;
return 0;
}