zoukankan      html  css  js  c++  java
  • 1030 完美数列(25)(25 分)

     

    给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列。

    现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完美数列。

    输入格式:

    输入第一行给出两个正整数N和p,其中N(<= 10^5^)是输入的正整数的个数,p(<= 10^9^)是给定的参数。第二行给出N个正整数,每个数不超过10^9^。

    输出格式:

    在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。

    输入样例:

    10 8
    2 3 20 4 5 1 6 7 8 9
    

    输出样例:

    8

    首先题意不难理解,就是先排序,再挨个筛查。但是这道题有非常多需要注意的细节
    第一,计算a[j]*q的时候会爆int类型,所以需要longlong,为了方便我把所有的int 都换成了longlong
    第二,double和int类型不能直接作比较,所以直接判断就行,a[i] > a[j]*q
    第三,第二层for循环要加上count,因为题目给的数据很大,会超时
    第四,第二层for循环int j = i+count;不能是int j = i+count+1;因为有可能就只有一个数,那么最大值最小值都是自己
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 bool cmp(int a, int b) {
     5     return a>b;
     6 }
     7 int maxN = 0;
     8 int main() 
     9 {
    10     long long n, q;
    11     cin >> n >> q;
    12     if(n == 0) {
    13         cout << 0;
    14         return 0;
    15     }
    16     long long a[n];
    17     long long x;
    18     long long count = 0;
    19     for(long long i = 0; i < n; i++) {
    20         cin >> x;
    21         a[i] = x;
    22     }
    23     sort(a, a+n, cmp);
    24     for(int i = 0; i < n; i++) {
    25         //不能是i+1+count,只有一个数的话最大最小都是自己 
    26         for(int j = i+count; j < n; j++) {
    27             if(a[i] > a[j]*q) {
    28                 break; 
    29             }  
    30             if(j-i+1>count) {
    31                 count = j-i+1;
    32             }
    33         } 
    34     }
    35     cout << count;
    36     return 0;
    37 }
    38 /*
    39 10 8
    40 2 3 20 4 5 1 6 7 8 9
    41 */
     
  • 相关阅读:
    Yield Usage Understanding
    Deadclock on calling async methond
    How to generate file name according to datetime in bat command
    Run Unit API Testing Which Was Distributed To Multiple Test Agents
    druid的关键参数+数据库连接池运行原理
    修改idea打开新窗口的默认配置
    spring boot -thymeleaf-url
    @pathvariable和@RequestParam的区别
    spring boot -thymeleaf-域对象操作
    spring boot -thymeleaf-遍历list和map
  • 原文地址:https://www.cnblogs.com/wzy-blogs/p/9150735.html
Copyright © 2011-2022 走看看