Skip to content

1030 完美数列

给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 Mm**p,则称这个数列是完美数列。

现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列。

输入格式:

输入第一行给出两个正整数 Np,其中 N(≤105)是输入的正整数的个数,p(≤109)是给定的参数。第二行给出 N 个正整数,每个数不超过 109。

输出格式:

在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。

输入样例:

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

输出样例:

tex
8
8

Solution:

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        int n = nextInt();
        long p = nextInt();
        long[] num = new long[n];
        for (int i = 0; i < n; i++) {
            num[i] = nextInt();
        }
        Arrays.sort(num);
        int result = 0;
        for (int i = 0; i < n; i++) {
            for (int j = result + i; j < n; j++) {
                if (num[j] <= num[i] * p) {
                    result = Math.max(result, j - i + 1);
                } else break;
            }
        }
        System.out.println(result);
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        int n = nextInt();
        long p = nextInt();
        long[] num = new long[n];
        for (int i = 0; i < n; i++) {
            num[i] = nextInt();
        }
        Arrays.sort(num);
        int result = 0;
        for (int i = 0; i < n; i++) {
            for (int j = result + i; j < n; j++) {
                if (num[j] <= num[i] * p) {
                    result = Math.max(result, j - i + 1);
                } else break;
            }
        }
        System.out.println(result);
    }
}

Released under the MIT License.