1030. Perfect Sequence (25) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Cao, Peng
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
Misunderstanding: The first as the minimum value of the results, this misunderstanding troubled for a long time, said it is still an example around the halo
For example
10 8
1 3 9 11 15 17 18 18 19 20
The answer is 9, starting from 3.3 9 11 15 17 18 18 19 20
Idea: This problem uses the Multiset container is most suitable, when we find the maximum number, we can reduce the number of cycles, such as
10 8
1 3 5 7 9 10 15 20 25 30
After 1 as a minimum, the maximum is 4, failure at 9,
The next time you make 3 minimum, directly from 9, because the second number is greater than or equal to the first number (ascending)
But to subtract num by 1 because the minimum value is shifted to the right one.
Thus saving the number of previous alignment
1 //1030.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <typeinfo>7#include <Set>8 9 using namespacestd;Ten One intMain () A { - intN, p, temp,num=0, max_num=0; -multiset<int>s; the -CIN >> N >>p; - - for(inti =0; i < N; ++i) + { -CIN >>temp; + A S.insert (temp); at } - -multiset<int>::iterator I, J, T=s.begin (), begin = S.begin (), end =s.end (); - intK, size =s.size (); - - for(k=1, i = begin; k<=size; ++k,++i) in { - for(j = t; J! = end; + +)j) to { + if(static_cast<Double> (*j)/P <= *i)//Use division to prevent cross-border -++num; the Else * Break; $ }Panax Notoginseng -t = j;//Keep this failure location the + if(Num >max_num) AMax_num =num; the +--num;//because the next loop is the number of the set in the back one, so minus 1 - } $ $cout << max_num <<Endl; - - return 0; the}
PAT B 1030 Perfect Series (+) C + + Edition