PAT 1085 Perfect Sequence[难]
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 (≤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 8
2 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 <iostream>
#include <vector>
using namespace std; int a[];
int main() {
int n,p,mn=1e9;
cin>>n>>p;
for(int i=;i<n;i++){
cin>>a[i];
if(a[i]<mn)
mn=a[i];
}
mn=p*mn;
int ct=;
for(int i=;i<n;i++){
if(a[i]<=mn)
ct++;
}
cout<<ct; return ;
}
Wrong
尝试用二分法写,依旧失败:
#include <iostream>
#include <algorithm>
using namespace std; int a[];
int main() {
int n,p;
cin>>n>>p;
for(int i=;i<n;i++){
cin>>a[i];
}
sort(a,a+n);//默认从小到大排序
p*=a[];
int left=,right=n-,mid;
while(left<=right){//最终结果是保存在mid里的。
mid=(left+right)/;
if(a[mid]==p)break;
if(p>a[mid])left=mid+;
else right=mid-;
}
cout<<mid+;
return ;
}
wrong2
代码转自:https://www.nowcoder.com/questionTerminal/dd2befeb7b6e4cea856efc8aa8f0fc1c
链接:https://www.nowcoder.com/questionTerminal/dd2befeb7b6e4cea856efc8aa8f0fc1c
来源:牛客网 #include <iostream>
#include <vector>
#include <algorithm> using namespace std; int main()
{
ios::sync_with_stdio(false);
// 读入数据
int N, p; cin >> N >> p;
vector<int> data(N);
for(int i=; i<N; i++) {
cin >> data[i];
} // 处理数据
sort(data.begin(), data.end());
int maxNum = ;
for(int i=; i<N; i++) {//这里是两层循环,
while(i+maxNum<N && data[i+maxNum]<=data[i]*p) {
maxNum++;
}
}
cout << maxNum << endl;
return ;
}
//这个思路是真的厉害。
1.计算maxNum,对每个i来说,如果长度小于那么肯定不会进入while循环,否则就可以++。
2.真是太厉害了,学习了。
代码来自:https://www.liuchuo.net/archives/1908
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int n;
long long p;
scanf("%d%lld", &n, &p);
vector<int> v(n);
for (int i = ; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end());
int result = , temp = ;
for (int i = ; i < n; i++) {
for (int j = i + result; j < n; j++) {
if (v[j] <= v[i] * p) {
temp = j - i + ;
if (temp > result)
result = temp;
} else {
break;
}
}
}
cout << result;
return ;
}
//好难理解啊,我得再思考思考+1.
PAT 1085 Perfect Sequence[难]的更多相关文章
- PAT 1085 Perfect Sequence
PAT 1085 Perfect Sequence 题目: Given a sequence of positive integers and another positive integer p. ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- PAT 甲级 1085 Perfect Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...
- PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]
题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- 1085. Perfect Sequence
Given a sequence of positive integers and another positive integer p. The sequence is said to be a “ ...
- 1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- PAT (Advanced Level) 1085. Perfect Sequence (25)
可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
随机推荐
- go context 讲解
控制并发有两种经典的方式,一种是WaitGroup,另外一种就是Context,今天我就谈谈Context. 什么是WaitGroup WaitGroup以前我们在并发的时候介绍过,它是一种控制并发的 ...
- hive执行更新和删除操作
Hive从0.14版本开始支持事务和行级更新,但缺省是不支持的,需要一些附加的配置.要想支持行级insert.update.delete,需要配置Hive支持事务. 一.Hive具有ACID语义事务的 ...
- nutch solr
创建solr数据目录 创建目录solrData,拷贝solr-4.10.2/example/solr到solrData下 修改配置文件中数据目录路径 修改 solrData/solr/coll ...
- java 线程之间的协作 wait()与notifyAll()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Could not load type ‘System.ServiceModel.Activation.HttpModule’ from&
1. 部署网站到IIS7.5,Window 2008的时候出现这个错误 2. 错误信息 Server Error in ‘/’ Application. Could not load type ‘ ...
- MFC多国语言——资源副本
此随笔主要参考了http://www.cnblogs.com/xianyunhe/archive/2011/09/02/2163842.html 为软件提供多国语言的支持的具体实现方法有很多,但基本原 ...
- phpStorm格式化代码快捷键
Ctrl+Alt+L
- 终于找到了最新的Chemdarw注册码
随着中国人对知识产权的保护意识提升,正版软件越来越流行,只有一小部分人还在寻找Chemdarw破解版.最新的ChemDraw 15正式版本已经强势来袭,在获取软件安装包之后需要有效的注册码才能激活软件 ...
- php数据库操作命令精华大全
1.表结构//列信息2.表数据//行信息3.表索引//把列中的行加到索引中(一般情况下一个表一定要把id这一列的所有数据都加到主键索引中) 2.[dos下]关闭mysql:net stop mysql ...
- linux配置sftp用户的chroot步骤(用户的sftp根文件夹)
1.编辑ssh中的sftp的配置,命令可能是:vi /etc/ssh/sshd_config 在这个文件里最后增加 #限制sftp组的用户使用sftp时在自己的home文件夹下 Match Group ...