1. 程式人生 > >PAT (Advanced Level) Practice 1085 Perfect Sequence (25 分)

PAT (Advanced Level) Practice 1085 Perfect Sequence (25 分)

子序列而非連續子序列,雙指標,列舉最大值,然後l指標不斷往左移

#include<cstdio>
#include<algorithm>
using namespace std;

const int N=1e5+5;

int a[N];

int main()
{
    int n,p;
    scanf("%d%d",&n,&p);
    for(int i=0;i<n;i++)
        scanf("%d",a+i);
    sort(a,a+n);
    int l=n-1,ans=0;;
    for(int
i=n-1;i>=0;i--) { while(l>=0&&1LL*a[l]*p>=a[i]) l--; ans=max(ans,i-l); } printf("%d\n",ans); return 0; }