1085. Perfect Sequence (25)

又一个线性扫描数组的例子

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  long long n,p;cin>>n>>p;
  vector<long long>v(n);
  for(auto&x:v)cin>>x;
  sort(v.begin(),v.end());
  long long pos=0,maxlen=0;
  for(long long i=0;i<n;++i){
    while(pos<n&&v[i]*p>=v[pos])++pos;
    maxlen=max(maxlen,pos-i);
  }
  cout<<maxlen;
}


你可能感兴趣的:(1085. Perfect Sequence (25))