PAT (Advanced Level) 1085. Perfect Sequence (25)

可以用双指针(尺取法),也可以枚举起点,二分终点。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

int n;
long long k;
long long a[100000 + 10];

int main()
{
    scanf("%d%lld", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    sort(a + 1, a + 1 + n);
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int p;
        int L = i, R = n;
        while (L <= R)
        {
            int mid = (L + R) / 2;
            if (a[mid] <= a[i] * k)
            {
                p = mid;
                L = mid + 1;
            }
            else R = mid - 1;
        }
        ans = max(ans, p - i + 1);
    }
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2016-07-02 22:36  Fighting_Heart  阅读(111)  评论(0编辑  收藏  举报