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

题目大意:给出一个序列,要求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[难]的更多相关文章

  1. PAT 1085 Perfect Sequence

    PAT 1085 Perfect Sequence 题目: Given a sequence of positive integers and another positive integer p. ...

  2. 1085 Perfect Sequence (25 分)

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  3. PAT 甲级 1085 Perfect Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...

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

  5. 1085. Perfect Sequence (25) -二分查找

    题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...

  6. 1085. Perfect Sequence

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a “ ...

  7. 1085 Perfect Sequence (25 分)

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...

  8. PAT (Advanced Level) 1085. Perfect Sequence (25)

    可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...

  9. 【PAT甲级】1085 Perfect Sequence (25 分)

    题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...

随机推荐

  1. UCI机器学习库和一些相关算法(转载)

    UCI机器学习库和一些相关算法 各种机器学习任务的顶级结果(论文)汇总 https://github.com//RedditSota/state-of-the-art-result-for-machi ...

  2. [转]Python定时任务框架APScheduler

    APScheduler是基于Quartz的 一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以 持久化任务 ...

  3. hdu5745 La Vie en rose 巧妙地dp+bitset优化+滚动数组减少内存

    /** 题目:hdu5745 La Vie en rose 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5745 题意:题目给出的变换规则其实就是交换相邻 ...

  4. MapReduce实战(一)自定义类型

    需求: 处理以下流量数据,第1列是手机号,第7列是上行流量,第8列是下行流量.将手机号一样的用户进行合并,上行流量汇总,下行流量也汇总,并相加求得总流量. 1363157985066 13726230 ...

  5. golang json数组拼接

    2016年06月16日 15:38:25 阅读数:2575 标签: golangjson数组 更多 个人分类: golang   func main() { a := []byte(`{"P ...

  6. js 控制按钮点击后不可用

    <input type="button" id="btn" value="免费获取验证码" /> <script type ...

  7. 【vijos】1750 建房子(线段树套线段树+前缀和)

    https://vijos.org/p/1750 是不是我想复杂了.... 自己yy了个二维线段树,然后愉快的敲打. 但是wa了两法.......sad 原因是在处理第二维的更新出现了个小问题,sad ...

  8. Unicode UTF-8 转换

    Unicode是类似“U+4E25”或“\u4E25”的编码方式,很多情况下是4个十六进制的数,有时候不止. Unicode编码系统可分为编码方式和实现方式两个层次: 编码方式:“严”的Unicode ...

  9. 复合文档(Compound Document)读写栗子

    复合文件是把磁盘文件系统的管理方式移植到文件中---复合文件. 复合文档是由 Windows 系统通过 COM 提供的, 它能完成像 Windows 目录结构一样复杂的文件结构的存取:提示一下 Win ...

  10. Dapper的语法应用

    (1)返回某个整型或字符串类型的字段 public string GetSupplierCodeById(int Id) { using( var conn=DbFactory.CreateConne ...