-
Notifications
You must be signed in to change notification settings - Fork 0
/
1085.cpp
103 lines (81 loc) · 1.69 KB
/
1085.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// ============================ 二分法 =================================
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int binaruSearch(int i, int n, long long x);
vector<int> seque(100001);
int main()
{
int n, p;
scanf("%d %d", &n, &p);
for (int i = 0; i < n; i++)
{
scanf("%d", &seque[i]);
}
sort(seque.begin(), seque.begin() + n);
int ans = 1; // 最大长度
for (int i = 0; i < n; i++)
{
// 在 a[i+1]~a[n-1] 中寻找第一个 > a[i]*p 的数,返回其位置
int j = binaruSearch(i, n, (long long)seque[i] * p);
ans = max(ans, j - i);
}
printf("%d", ans);
return 0;
}
// 在 [i+1,n-1] 的范围内查找第一个大于x的数的位置
int binaruSearch(int i,int n, long long x)
{
// 如果所有数都<=x,则返回n
if (seque[n - 1] <= x)
return n;
int left = i + 1;
int right = n - 1;
int mid; // 在[i+1,n-1]范围内查找
while (left < right)
{
mid = (left + right) / 2;
if (seque[mid] > x)
{
right = mid;
}
else //seque[mid] <= x
{
left = mid + 1;
}
}
return left; // while 结束时 left == right,返回任意一个均可
}
// ============================== two pointers ===================================
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100010];
int main()
{
int n, p;
scanf("%d %d", &n, &p);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
// 从小到大排序
sort(a, a + n);
// two pointers
int i = 0;
int j = 0;
int maxLen = 0;
long long res;
while (i < n && j < n)
{
res =(long long) a[i] * p;
while (a[j] <= res && j < n)
j++;
maxLen = (j - i) > maxLen ? (j - i): maxLen;
i++;
}
printf("%d", maxLen);
return 0;
}