1030 完美数列

 two pointers通向扫描法。

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a[100010] = {0};
int main() {
    int n,p,max = -1;
    cin>>n>>p;
    for(int i = 0; i < n; ++i)
        cin>>a[i];
    sort(a,a+n);//递增排序
    int i = 0,j = 0;
    while(i < n) {//two poniters同向扫描 
        while(j < n && a[j]<= a[i]*p)//用int型的话a[i]*p会溢出,所以数组定义成了long long类型
            j++;
        if(max < j-i)
            max = j-i;
        i++;
    }
    cout<<max;
    return 0;
}

 

posted @ 2020-02-18 12:06  tangq123  阅读(129)  评论(0编辑  收藏  举报