Cattle off network PAT B Zhenti perfect number of columns (25)

Cattle off network PAT B Zhenti perfect number of columns (25)

Time limit 1000 ms memory limit 32768 KB code size limit 100 KB Analyzing Program Standard (from small)
Title Description
Given a positive integer sequence, and positive integers p, provided the maximum number of columns that 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 Description:
input of the first line gives two positive integers N and p, where N (<= 105) is the number of input positive integer, p (<= 109) are given parameters. The second line gives a positive integer N, the number of each
not more than 109.

Description Output:
output number can select the maximum number of them may be used in series form a perfect line.

Examples Input:
10. 8
2 20 is. 4. 3. 1. 5. 6. 7. 8. 9

Examples Output:
8

AC code is as follows:

#include<stdio.h>
#include<stdlib.h>
//嘤嘤嘤,只有最后一个例子超时了!
int cmp(const void *a,const void *b)
{
    if( *(long *)a!=*(long *)b ) return *(long *)a-*(long *)b;
    else return 1;
}
int main()
{
    long a[100001];
    int N,p,len,i,j;
    scanf("%d%d",&N,&p);
    for(i=0;i<N;i++) scanf("%ld",&a[i]);
    qsort(a,N,sizeof(a[0]),cmp);        //已经排序完成

    for(len=1,i=0;i<N-len;i++)
    {
        int templen=len-1;
        for(j=i+len;j<N;j++)
        {
            if(a[j]<=a[i]*p)
                templen++;
            else break;//哎呀呀!就是加了这一句,最后一个case才过的
        }//时间复杂度真的很重要!
        if(templen>len)len=templen+1;
    }
    printf("%d\n",len);
    return 0;
}

Published 18 original articles · won praise 2 · Views 222

Guess you like

Origin blog.csdn.net/zhou_hao_ran/article/details/103788856