zoukankan      html  css  js  c++  java
  • PAT-A 1085. Perfect Sequence

    1085. Perfect Sequence

    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
    

    程序代码:

    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 100000
    long long num[MAX];
    void sort(long long n[],int len);
    void quicksort(long long r[],int start,int end);
    int comp(const void* a,const void* b)
    {
        if(*(long long*)a<*(long long*)b)   return -1;
        if(*(long long*)a==*(long long*)b)      return 0;
        if(*(long long*)a>*(long long*)b)       return 1;
    }
    int main()
    {
        int N,i,j;
        long long p;
        long long sum ;
        long long m;
        int max = 0;
        int count=0;
        scanf("%d%lld",&N,&p);
        for(i=0;i<N;i++)
            scanf("%lld",&num[i]);
        qsort(num,N,sizeof(long long),comp);
        for(i=0;i<N;i++)
        {
            sum = num[i]*p;
            for(j=i+count;j<N;j++)
            {
                if(num[j]<=sum)
                {
                    count =j-i+1;
                }
                else
                    break;
            }
            if(count > max)
                max = count;
        }
        printf("%d",max);
        return 0;
    }
    void sort(long long n[],int len)
    {
        int i,j;
        long long tmp;
        for(i=len-1;i>0;i--)
            for(j=0;j<i;j++)
            {
                if(n[j]>n[j+1])
                {
                    tmp = n[j];
                    n[j]=n[j+1];
                    n[j+1]=tmp;
                }
            }   
    }
    void quicksort(long long r[],int start,int end)
    {
        int i=start;
        int j=end;  
        long long temp=r[start];
        if(i<j)
        {
            while(i!=j)
            {
                while(i<j&&r[j]>=temp)
                    j--;        
                if(i<j)
                {
    
                    r[i]=r[j];
                    i++;
                }       
                while(i<j&&r[i]<temp)
                    i++;        
                if(i<j)
                {
                    r[j]=r[i];
                    j--;
                }   
            }   
            r[i]=temp;  
            quicksort(r,start,i-1);
            quicksort(r,i+1,end);
        }           
    }
    
    
  • 相关阅读:
    Path Sum II
    Convert Sorted Array to Binary Search Tree
    Construct Binary Tree from Inorder and Postorder Traversal
    Construct Binary Tree from Preorder and Inorder Traversal
    Maximum Depth of Binary Tree
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Same Tree
    Validate Binary Search Tree
    Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/zhengkang/p/5734311.html
Copyright © 2011-2022 走看看