【剑指紫金港】1085 Perfect Sequence

A 1085 Perfect Sequence

题目链接

Problem Description

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

Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤10​5​​ ) is the number of integers in the sequence, and p (≤10​9​​ ) is the parameter. In the second line there are N positive integers, each is no greater than 10​9​​ .

Output

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

题目大意

一个数字序列,最大的数是M,最小的数是m,给出一个数p。问从中最多取出多少个数组成一个序列,满足M≤m×p

解题思路

先对输入的数字序列排序,然后设立下标r从后往前搜索,找到第一个符号条件的序列头下标l。此时以r为最大数的序列长度最长是(r-l+1)。

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;

ll a[maxn],p,now;
int n,ans=0;

int magic(ll x,int r){
    int l=0,mid;
    while(l<r){
        mid=(l+r)/2;
        if(a[mid]>=x){
            r=mid;
        }else{
            l=mid+1;
        }
    }
    return l;
}

int main(){
	scanf("%d%lld",&n,&p);
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	sort(a,a+n);
	int l=0,r=n-1;
	while((r+1)>ans){ //如果从0开始计的最长长度都不比当前最长长度长,终止
		l=0;
        if(a[r]%p==0){
            now=a[r]/p;
        }else{
            now=a[r]/p+1;
        }
        l=magic(now,r); //手写lower_bound函数,找到第一个满足条件的l的下标
		ans=max(r-l+1,ans);
		r--;
	}
	printf("%d",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

征服所有不服

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

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

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

打赏作者

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

抵扣说明:

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

余额充值