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. Tomcat服务器的默认端口是多少?怎样修改tomcat的端口?

    Tomcat服务器的默认端口是多少?怎样修改tomcat的端口? 解答:默认端口为8080,可以通过service.xml的Connector元素的port属性来修改端口.

  2. spring mvc +easy ui +Mybatis 录入数据

    1.itemsEasyui.jsp 应用到的插件及知识点:日期控件My97 ,图片本地预览函数PreviewImage() (1)easy ui 的模态窗口使用时,要指定DIV的属性 data-opt ...

  3. java算法-数学之美二

    上一章已经说过利用数学思想来解决程序算法问题,实际上就是找规律.这在我们上学时经常遇到,比如给出一段数字,求某一个位置该填写什么数,只要找到规律那就迎刃而解.好了,废话不多说,再来看看案例分析.    ...

  4. [翻译]在ASP.NET Web API中通过OData支持查询和分页

    OData可以通过形如http://localhost/Products?$orderby=Name这样的QueryString传递查询条件.排序等.你可以在任何Web API Controller中 ...

  5. C#基础语言知识--编译和执行过程

    http://blog.csdn.net/stive_sourcexin/article/details/51329697

  6. Web API中的模型验证Model Validation

    数据注释 在ASP.NET Web API中,您可以使用System.ComponentModel.DataAnnotations命名空间中的属性为模型上的属性设置验证规则. using System ...

  7. c++ new(不断跟新)

    1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...

  8. linux jdk 6 版本下载

    http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-41940 ...

  9. 【BZOJ1189】[HNOI2007]紧急疏散evacuate 动态加边网络流

    [BZOJ1189][HNOI2007]紧急疏散evacuate Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空 ...

  10. Python基础——原生数据类型(字典,列表,元组,字符串)

    字典Dictionary 字典定义了键值对的1对1管理. 1.定义字典 请看下面的栗子,我们先创建了一个具有两个元素的字典,每对都是key:value的形式. 我们能通过key得到value,但是不能 ...