博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1085 Perfect Sequence[难]
阅读量:4705 次
发布时间:2019-06-10

本文共 3093 字,大约阅读时间需要 10 分钟。

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 Mm×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 (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​​.

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 82 3 20 4 5 1 6 7 8 9

Sample Output:

8

 题目大意:给出一个序列,要求M<=m*p,M是最大值,m是最小值,从序列中找到尽可能多的数满足这个公式。

//比如对给的样例:序列最大可以包含8个数,分别是 2 3 4 5 1 6 7 8就是这样。 但是最小也不一定是从序列最小值开始的,就是截取序列的一部分。

 这是我一开始写的,理解错题意了。还以为是最小值确定,找出集个最大值呢满足即可。牛客网上通过率为0,PAT上得了5分通过了3个测试点。

#include 
#include
using namespace std;int a[100001];int main() { int n,p,mn=1e9; cin>>n>>p; for(int i=0;i
>a[i]; if(a[i]
Wrong

 

尝试用二分法写,依旧失败:

#include 
#include
using namespace std;int a[100001];int main() { int n,p; cin>>n>>p; for(int i=0;i
>a[i]; } sort(a,a+n);//默认从小到大排序 p*=a[0]; int left=0,right=n-1,mid; while(left<=right){
//最终结果是保存在mid里的。 mid=(left+right)/2; if(a[mid]==p)break; if(p>a[mid])left=mid+1; else right=mid-1; } cout<
wrong2

 

代码转自:https://www.nowcoder.com/questionTerminal/dd2befeb7b6e4cea856efc8aa8f0fc1c

链接:https://www.nowcoder.com/questionTerminal/dd2befeb7b6e4cea856efc8aa8f0fc1c来源:牛客网#include 
#include
#include
using namespace std;int main(){ ios::sync_with_stdio(false); // 读入数据 int N, p; cin >> N >> p; vector
data(N); for(int i=0; i
> data[i]; } // 处理数据 sort(data.begin(), data.end()); int maxNum = 0; for(int i=0; i

 

//这个思路是真的厉害。

1.计算maxNum,对每个i来说,如果长度小于那么肯定不会进入while循环,否则就可以++。

2.真是太厉害了,学习了。

代码来自:https://www.liuchuo.net/archives/1908

#include 
#include
#include
using namespace std;int main() { int n; long long p; scanf("%d%lld", &n, &p); vector
v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); int result = 0, temp = 0; for (int i = 0; i < n; i++) { for (int j = i + result; j < n; j++) { if (v[j] <= v[i] * p) { temp = j - i + 1; if (temp > result) result = temp; } else { break; } } } cout << result; return 0;}

 

//好难理解啊,我得再思考思考+1.

转载于:https://www.cnblogs.com/BlueBlueSea/p/9781602.html

你可能感兴趣的文章
huffman编码压缩和解压
查看>>
ssm基础配置
查看>>
jquery 自动运行JS 和如何点击标签运行js 及淡入,淡出效果时 如何附加JS函数
查看>>
mysql备份数据库出错mysqldump: [ERROR] unknown option '--no-beep'
查看>>
vim替换^m字符
查看>>
黑马程序员——网络编程
查看>>
Android底层开发技术实战详解——内核、移植和驱动
查看>>
《软件测试实战:微软技术专家经验总结》
查看>>
《学习OpenCV》课后习题解答6
查看>>
UIDatePicker
查看>>
在XP系统中发布MVC3项目nopCommerce2.65及配置
查看>>
PHP开发异步高性能的MySQL代理服务器
查看>>
杭电之统计汉字
查看>>
笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-07 透视、逆透视及分组集
查看>>
贝叶斯分类器
查看>>
<img>标签
查看>>
android:intent flags
查看>>
Vue疑难杂症
查看>>
spring boot 错误处理之深度历险
查看>>
Linux--Centos7开机启动 mysql5.7.19
查看>>