pat1030.--Perfect Series

Source: Internet
Author: User

Given a positive integer sequence, and a positive integer p, the maximum value in this sequence is m, the minimum is m, and if M <= m * p, then this sequence is called the perfect sequence.

Now given the parameter p and some positive integers, you can choose as many numbers as possible to make a perfect sequence.

Input format:

Enter the first line to give two positive integers n and p, where N (<= 105) is the number of positive integers entered, and P (<= 109) is the given parameter. The second line gives n positive integers, each with a number not exceeding 109.

Output format:

You can select the maximum number of rows in a row to make a perfect sequence.

Input Sample:

10 82 3 20 4 5 1 6 7 8 9

Sample output:

8
In the beginning no Brain O (N2) timeout, and then optimize, J do not start from I, continue from the last place, because the array is already sorted. One less variable is correct, and there is no finding where the original mistake was.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cctype>#include<cstdlib>#include<cmath>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#include<cctype>using namespacestd;typedef unsignedLong Longull;#defineINF 0XFFFFFFFLong Longa[100000];intMain () {Long Longn,x,y,i,j,k; Memset (A,0,sizeof(a)); CIN>>x>>y;  for(i=0; i<x;++i) Cin>>A[i]; Sort (A,a+x); K=1; J=0;  for(i=0; i<x;++i) { while(a[j]<=a[i]*y&&j<x) {++J; } k=max (k,j-i); } cout<<k<<Endl; return 0;}

pat1030.--Perfect Series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.