查看其它博客的方法,发现还有个distance方法
distance (InputIterator first, InputIterator last); 在c++中,distance为一函数模版,返回两个迭代器间的距离。
#include<iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main() {
ll n, p, t, tmp;
scanf("%lld%lld", &n, &p);
ll v[n];
for (ll i = 0; i < n; ++i) {
scanf("%lld", &tmp);
v[i] = tmp;
}
sort(v, v+n);
int ans = 0;
for (int i = 0; i < n; ++i) {
t = p * v[i];
int tmp = upper_bound(v, v+n, t)-v-i;
ans = max(ans, tmp);
}
printf("%d\n", ans);
return 0;
}