PAT Basic 1030 perfect series (25 points)

Given a positive integer sequence, and positive integers  p, provided that the maximum number of columns is  M, the minimum value is  m, if  M m p, the number of columns is called a perfect sequence.

Now given parameter  p and a number of positive integers, please choose as many numbers constitute a perfect series.

Input formats:

Input of the first line gives two positive integers  N and  p, where  N ( ≤) is the number of input positive integer, P ( ≤) is given parameters. The second line gives  N positive integers, each number is not more than  1

Output formats:

In line output you can select up to number how many they can form a perfect series.

Sample input:

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

Sample output:

8



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

int main()
{
    int N,p;
    cin>>N>>p;
    long long a[N];
    for(int i=0;i<N;i++)
        cin>>a[i];
    sort(a,a+N);
    int res=0;
    for(int i=0;i<N;i++){
        for(int j=i+res;j<N;j++){
            if(a[j]<=a[i]*p) res++;
            else break;
        }
    }
    cout<<res;
    system("pause");
}

A time-out point analysis:

1. If you do not use two cycles, then the concept is wrong

Test Case:

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

2. a time-out, you probably do not add else break, this else break meaningful, let your complexity becomes O (1)

Test: Take the number 10 ^ 5 allows you to slowly traverse

3 does not use long long, 10 ^ 9 to 10 digits, not 9, int the range of 2 to 8 behind the number, size may exceed

Test: put a intermediate 3 (8 0), there is a number of overflows

Guess you like

Origin www.cnblogs.com/littlepage/p/11444487.html