1085 Perfect Sequence (25 分)

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 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 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

第一种方法:

用max记录当前的最长序列长度,因为是要找出最大长度,因此对于每个i,只需要让j从i + max开始。

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> v; int main()
{
int N, i, j;
long p;
cin >> N >> p;
v.resize(N);
for (i = ; i < N; i++) cin >> v[i];
sort(v.begin(), v.end());
int t, max = ;
for (i = ; i < N; i++)
{
for (j = i + max; j < N && v[j] <= v[i] * p; j++);
if (j - i > max) max = j - i;
}
cout << max;
return ;
}

关于lower_bound()和upper_bound()的使用:

在从小到大的排序好的数组中:

(lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

来源:CSDN
原文:https://blog.csdn.net/qq_40160605/article/details/80150252 )

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> v; int main()
{
int N, i, j;
long p;
cin >> N >> p;
v.resize(N);
for (i = ; i < N; i++) cin >> v[i];
sort(v.begin(), v.end());
int t, max = ;
for (i = ; i < N; i++)
{
t = upper_bound(v.begin() + i, v.end(), v[i] * p) - (v.begin() + i);
if (t > max) max = t;
}
cout << max;
return ;
}

参考:

https://www.liuchuo.net/archives/1908

最新文章

  1. 【Xpath学习】xpath都不会,说什么你做网站自动化测试的?
  2. 【Python】supervisor安装和管理celery
  3. Python为什么不隐式实现self
  4. div在浏览器窗口中居中
  5. 自定义泛型N维空间数组
  6. Hadoop入门进阶课程8--Hive介绍和安装部署
  7. VirtualBox的网络设置(6种方式)
  8. cobbler
  9. Spark on Yarn
  10. 通过SQL Server Profiler来监视分析死锁
  11. 初学java之接口基础
  12. Transact-SQL 语句
  13. HDU1875 畅通工程再续 (并查集)
  14. iOS中UITextView键盘回收
  15. Spring Boot 探索系列 - 自动化配置篇
  16. for循环,数组
  17. 通过IF({1,0}和VLOOKUP函数实现Excel的双条件多条件查找的方法
  18. IoC和AOP的理解
  19. laravel5.5 env
  20. CuratorFramework使用

热门文章

  1. java算法外传之靠工资多久能实现小目标...
  2. stark组件之创建
  3. python+splinter实现12306网站刷票并自动购票流程
  4. @Value(&quot;#{}&quot;)与@Value(&quot;${}&quot;)的区别
  5. Nuxt 2.3.X 配置babel
  6. Linux 进程间通信之管道(pipe),(fifo)
  7. spark常用算子总结
  8. Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String
  9. (转)TCP连接的11种状态变迁
  10. pat1025. PAT Ranking (25)