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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章