查看其它博客的方法,发现还有个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;
}
本文详细介绍了C++中的distance函数,这是一个用于计算两个迭代器间距离的模板函数。通过一个具体的代码示例,展示了如何使用distance函数来解决特定问题,包括输入处理、排序、上界查找以及结果输出等步骤。
580

被折叠的 条评论
为什么被折叠?



