题目链接:
思路:找到两个序列中最大的数和最小的数,并组成一个新的序列,尽量使序列的长度长一些,并且满足
mi*p<=mx,一开始我想现将序列从小到大排序,然后用n*n的嵌套循环来做,思路没错,就是时间超限了,后来参考了网上的做法,发现可以直接在序列中找大于mi*p的值,用二分法就行了,(看来要序列超时就想想二分)。
补充:upper_bound()的用法
头文件:#include<algorithm>
upper_bound(起始地址,终止地址,查找的值),返回值是数值的第一个出现的位置,
lower_bound(起始地址,终止地址,查找的值),返回的是数值的第一个出现的位置。
bindary_search(起始地址,终止地址,查找的值),返回的是false或true,如果要找的值存在,返回true,否则返回false
注意:如果lower_bound()是左闭右开查找,如果要记录所找的位置,要减去a.begin()。
upper_bound参考文章:http://www.cnblogs.com/Tang-tangt/p/9291018.html
pat参考文章:https://blog.csdn.net/TateBrwonJava/article/details/83582854
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100010;
typedef long long LL;
LL a[maxn],p,mx;
int main(void)
{
int l,r,i,j,n;
scanf("%d%lld",&n,&p);
for(i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n);
mx=0;
for(i=0;i<n;i++)
{
l=i;
r=upper_bound(a,a+n,a[i]*p)-a;
if(r-l>mx) mx=r-l;
}
printf("%lld\n",mx);
return 0;
}