PAT (Advanced Level) Practice 1085 Perfect Sequence (25 分) 凌宸1642

PAT (Advanced Level) Practice 1085 Perfect Sequence (25 分) 凌宸1642

题目描述:

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.

译:给定一个正整数序列和另一个正整数 p。如果满足 M ≤ m × p , 其中 M 和 m 分别为这个序列中的最大值和最小值,则这个序列称为完美序列。

现在给定一个序列和参数 p ,你应该尽可能多找出能够组成完美序列的数字。


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.

译:每个输入文件包含一个测试。对于每个用例,在第一行中给出两个正整数 N 和 p , N (≤105) 是序列包含的数字个数, p (≤109) 是参数。在第二行中给出 N 个正整数,并且都不超过 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

The Idea:

首先对 N 个数字进行从小到大排序,然后从左到右遍历,以左边为最小值,找到满足要求的最大值的位置,即可确定此时的完美序列的长度。当遍历完整个数字,即可找到最大的完美序列长度。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
ll n , p , ans = 0 ;
int main(){
	cin >> n >> p ;
	ll v[n] ;
	for(int i = 0 ; i < n ; i ++) cin >> v[i] ; 
	sort(v , v + n) ;
	for(int i = 0 ; i < n ; i ++){
		ans = max(ans , (ll)(upper_bound(v, v + n , v[i] * p) - v) - i ) ; // 利用 upper_bound()函数找到当前满足要求得最大值的位置
	}
	cout << ans  <<endl ; 
	return 0 ;
}
上一篇:flexton vendor 的screening


下一篇:一次尴尬的ORACLE安装