美文网首页
1085 Perfect Sequence(排序)

1085 Perfect Sequence(排序)

作者: virgilshi | 来源:发表于2018-09-29 10:13 被阅读0次

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 (≤109) 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

题目大意

给定一个序列A和参数p,若序列A中最大值为M,最小值为m,满足M<=m*p,则称序列A为perfect sequence;依据这个定义,题目要求在给定的序列中找出尽可能多的元素组成一个新的序列,使得这个序列为perfect sequence。

分析

寻找序列极值问题,若对算法的复杂度无苛刻要求,可先对序列排序,定义当前最大宽度maxwidth,遍历序列,遍历过程中以当前maxwidth为起始宽度寻找比maxwidth更大宽度的序列。另外,题目中序列元素和参数p的范围均为0~109,两数想成数的范围将可能超过109,因此将数的类型定义为long int

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v;
int main(){
    long long n,p;
    cin>>n>>p;
    v.resize(n);
    for(int i=0;i<n;i++) cin>>v[i];
    sort(v.begin(), v.end());
    int maxwidth=1;
    for(int i=0;i<n;i++){
        while(i+maxwidth-1<n && v[i+maxwidth-1]<=p*v[i]) maxwidth++;
    }
    cout<<maxwidth-1;
}

相关文章

  • 1085 Perfect Sequence(排序)

    1085 Perfect Sequence (25 分) Given a sequence of positive...

  • 1085. Perfect Sequence (25)

    PAT-A 1085,题目地址:https://www.patest.cn/contests/pat-a-prac...

  • 1085 Perfect Sequence (25 分)(使用队

    注意10^9 * 10^9会整形溢出,所以队列存的数不能int类型,得是long long类型; 不借助队列,简洁版本

  • B1085 Perfect Sequence(二分)

    题意:寻找最长的完美序列,也就是一串数字中的min*q>=max 先排序,然后枚举起点a[i]*q,在序列中寻找第...

  • 算法导论 第一章 插入与归并排序

    算法排序:input:sequence 《a1,a2,a3…..an》output: permutation 《a...

  • 2018-04-05

    It may not be perfect but it's perfect and perfect in my ...

  • iOS相册Moment功能的优化方案

    最近在开发公司产品Perfect365的Gallery模块, 包括按日期排序的Moment以及Album这两个模块...

  • 1085

    观自在菩萨,行深般若波罗蜜多时,照见五蕴皆空,度一切苦厄,舍利子,色不异空,空不异色。色即是空,空即是色。受想行识...

  • 1085

    9月14日,农历八月十九,雨,周三 台风来了,晚上比白天凉快许多。眼睛疼,早点呼呼吧!

  • perfect perfect

    好久没有打游戏,这几天,那种perfect感爆棚了,简直。 啥perfect?就是那种你做的每件事都完全在掌控范畴...

网友评论

      本文标题:1085 Perfect Sequence(排序)

      本文链接:https://www.haomeiwen.com/subject/smrvoftx.html