PAT Level B 1030, with detailed question analysis, if you don’t have an AC, don’t bother.

1030 perfect sequence (25 points)

Given a positive integer sequence and a positive integer p , let the maximum value in this sequence be M and the minimum value be m . If Mmp , then this sequence is called a perfect sequence.

Now given the parameter p and some positive integers, please select as many numbers as possible to form a perfect sequence.

Input format:

The first line of input gives two positive integers N and p , where N (≤105

) is the number of positive integers entered, p (≤109

) is the given parameter. The second line gives N positive integers, each number does not exceed 109

Output format:

Output in one line the maximum number of numbers that can be selected to form a perfect sequence.

Input example:

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

Output sample:

8

Question analysis:
Note that p and the given N positive integers are both <10^9, and long storage should be used. (last test point)

① Sort N positive integers from small to large 1 2 3 4 5 6 7 8 9 20 nums[] array storage

② Start looping from left to right, find the subscript of j where num[j]>nums[i]*p, and then use ji to find out how many numbers there are in total. Attached code:

//看这段代码的时候可以拿出纸和笔来操作一下,是整道题的核心
int max=0,result=0;//max记录最大个数,result中间变量
for(int i=0;i<N;i++){
    
    
    //设置i+max可以缩短循环时间,max比如为4,
    // 只需考虑i+4之后的数,之前的数一定小于4位
    for(int j=i+max;j<N;j++){
    
    
        if(nums[j]<=nums[i]*p){
    
    
            result=j-i+1;//记录一下当前j和i之间有几个数
            //如果max小于result,令max=result
            if(max<result)
                max=result;
         }else //这说明nums[j]>nums[i]*p,不满足M<=mp,结束循环
             break;
    }
}

③Output max value
without AC code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
    
    
    public static void main(String[] args) throws Exception{
    
    
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        int N=Integer.parseInt(str[0]);
        //用long存储
        long p=Long.parseLong(str[1]);
        String[] st=br.readLine().split(" ");
        long nums[]=new long[N];//用long
        for(int i=0;i<N;i++){
    
    
            nums[i]=Long.parseLong(st[i]);
        }
        Arrays.sort(nums);//排序
        
        //这道题的核心代码
        int max=0,result=0;
        for(int i=0;i<N;i++){
    
    
            for(int j=i+max;j<N;j++){
    
    
                if(nums[j]<=nums[i]*p){
    
    
                    result=j-i+1;
                    if(max<result)
                        max=result;
                }else
                    break;
            }
        }
        
        //输出
        System.out.print(max);
    }
}

My method of finding max for this question is already very fast. I tried to switch to a faster sorting method, but test point 4 still times out. The unfriendliness of PAT towards JAVA is once again reflected. Again, if this happens to everyone, don’t waste time deducting full points.
If there is a faster way, please leave a message in the comment area.

For more topic analysis, follow the public account Algorithm Baby

Guess you like

Origin blog.csdn.net/CSDN_Lrcx/article/details/116168414