1085 Perfect Sequence (25point(s)) - C語言 PAT 甲級

1085 Perfect Sequence (25point(s))

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.

Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤105) is the number of integers in the sequence, and p (≤109) is the parameter. In the second line there are N positive integers, each is no greater than 109.

Output Specification:

For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.

Sample Input:

10 8
2 3 20 4 5 1 6 7 8 9

Sample Output:

8

題目大意:

1030 完美數列 (25point(s))

設計思路:

1030 完美數列(C語言)

  • 先快速排序原數列,從最小值遍歷尋找最大的完美數列
  • 題意 m 和 M 是對於完美數列的,這樣 m 不同, M 也會變化,纔有不同長度的完美數列
    最初以爲找到原始數列的最小值,並以此判斷 M 來尋找完美數列(卒~)
編譯器:C (gcc)
#include <stdio.h>
#include <stdlib.h>

int comp(const void *a, const void *b);

int main()
{
    long long p, num[100000], min, max;
    int n, count = 0, i;
    scanf("%d %lld", &n, &p);
    for(i = 0; i < n; i++){
        scanf("%lld", &num[i]);
    }

    qsort(num, n, sizeof(long long), comp);

    for(min = 0, max = 0; max < n && count < n - min; min++){
        while(max < n && num[max] <= num[min] * p){
            max++;
        }
        if(count < max - min){
            count = max - min;
        }
    }
    printf("%d\n", count);

    return 0;
}

int comp(const void *a, const void *b)
{
    return *(long long *)a - *(long long *)b;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章