题目
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 is the number of integers in the sequence, and is the parameter. In the second line there are N positive integers, each is no greater than .
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:
1 | 10 8 |
Sample Output:
1 | 8 |
题解
思路
- 题目的意思是
- 给定一个序列和一个参数
- 最多从序列中拿多少项
- 能使得最大值除以最小值小于等于参数
- 我们将序列从小到大排列
- 遍历这个序列
- 不断更新符合要求的最大距离即可
- 注意C++要使用long int,int范围无法表示那么大的数量级。
数据结构
- n 为列表长度
- para 为给定参数
- seq 为序列
- ans 为答案
算法
- 读入列表
- 按从小到大排序
- 遍历每一个数
- 当这个数到最后一个数的距离大于ans的时候,说明还存在更新ans的可能
- 当这个数和距离ans位置以后的数符合题目的最大最小值限定的时候
- ans + 1
- 输出ans即可
代码
- 因为Python会有一个点过不了,因此也放了C++的解
Python
1 | n, para = list(map(int, input().split())) |
C++
1 |
|