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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值