算法训练

编程入门 行业动态 更新时间:2024-10-06 16:24:33

<a href=https://www.elefans.com/category/jswz/34/1770096.html style=算法训练"/>

算法训练

7-2 Perfect Sequence (50分)
7-2完全序列(50分)

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

C++代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>typedef long long ll;
using namespace std;
const int N=1e5+10;
const int maxn=1e5+10;
int main()
{int n,mx=0;ll p;scanf("%d%lld",&n,&p);vector<ll>v(n);for(int i=0;i<n;i++)scanf("%lld",&v[i]);sort(v.begin(),v.end());for(int i=0;i<n;i++){int ls=upper_bound(v.begin(),v.end(),v[i]*p)-v.begin();if((ls-i)>mx)mx=ls-i;}cout<<mx<<endl;return 0;
}

小提示:

比赛前可以先将系统头文件都写到电脑记事本里,拿到题目后直接粘贴,对代码运行时间没有影响,
因为不调用相应头文件里的函数,就不会运行相应的头文件。

更多推荐

算法训练

本文发布于:2024-02-13 21:46:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1761061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!