美文网首页
B1030 完美数列 (25分)

B1030 完美数列 (25分)

作者: km15 | 来源:发表于2020-02-06 11:20 被阅读0次

    题意:
    1、给定N个数,和一个权重P,要求,最大值M<=mp(最小值乘以p),求着N个数中最多可以构成完美数列的数字个数

    解题:
    首先性质,如果a[j] <= a[i] *p,那么对[i,j]内所有数,式子成立
    1、先排序,从小到大
    2、令i和j为0,并设置一个计数器
    3、令j在满足式子的同时,不断往前移,知道不成立,i++,接着循环这个过程,其中不断更新count
    4、终止条件是,j到达末尾了

    learn && wrong:
    1、性质,
    2、while里面这个写法不错

    #include <iostream>
    #include <algorithm> 
    
    using namespace std;
    
    const int maxn = 100010;
    int main(int argc, char** argv) {
        int n,p,a[maxn];
        scanf("%d%d",&n,&p);
        
        for(int i = 0;i < n;++i){
            scanf("%d",&a[i]);
        } 
        
        sort(a,a+n);
        int i = 0,j = 0, count = 1;
        while(i < n && j < n){
            //J不断右移,直到恰好不满足条件
            while(j < n && a[j] <= (long long) a[i] * p){
                j++;
                count = max(count,j - i);
            } 
            i++;
        }
        
        printf("%d\n",count);
        
        return 0;
    }

    相关文章

      网友评论

          本文标题:B1030 完美数列 (25分)

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