0%

nowcoder 小白月赛41 D题(二分)

题目链接
注意边界

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 300010;
ll n, k;
ll a[N];
ll ans1, ans2, ans3;

int lower_find(int l, int r, int val) { //不能滥用lower_bound();
while(l <= r) {
int mid = l + r >> 1;
if(a[mid] < val) l = mid + 1;
else r = mid - 1;
}
return l;
}
int upper_find(int l, int r, int val) {
while(l <= r) {
int mid = l + r >> 1;
if(a[mid] <= val) l = mid + 1;
else r = mid - 1;
}
return l;
}

int main() {
cin >> n >> k;
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++) {
if(a[i] * a[i+1] > k) ans1 += n - i;
else if(a[i] * a[n] < k) ans3 += n - i;
else {
int pos1 = lower_find(i + 1, n, k / a[i]);
int pos2 = upper_find(i + 1, n, k / a[i]);
if(k % a[i] != 0) {
ans3 += pos2 - i - 1;
ans1 += n - pos2 + 1;
} else {
ans3 += pos1 - i - 1;
ans1 += n - pos2 + 1;
ans2 += pos2 - pos1; //***
}
}
}
cout << ans1 << " " << ans2 << " " << ans3 << endl;

return 0;
}
求大佬赏个饭