zoukankan      html  css  js  c++  java
  • 1085 Perfect Sequence

    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 (≤) is the number of integers in the sequence, and p (≤) is the parameter. In the second line there are N positive integers, each is no greater than 1.

    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和最小值n,满足M <= n * p。刚开始的时候我就理解错了,没有搞明白题意,以为是按照所给出序列的顺序来寻找。

    思路:

    https://www.liuchuo.net/archives/1908

    Code:

    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    int main() {
        int t;
        long long p;
        cin >> t >> p;
        int c;
        vector<int> v;
        for (int i = 0; i < t; ++i) {
            cin >> c;
            v.push_back(c);
        }
        sort(v.begin(), v.end());
        int temp = 0, result = 0;
        for (int i = 0; i < t; ++i) {
            for (int j = i+result; j < t; ++j) {
                if (v[j] <= v[i]*p) {
                    temp = j - i + 1;
                    if (temp > result) 
                        result = temp;
                } else {
                    break;
                }
            }
        }
        cout << result << endl;
        return 0;
    }
    

      

    int的取值范围:-2147483648 到2147483647

    这里p的范围是小于109已经超出了int的范围,所以应该选择long long

                if (v[j] <= v[i]*p) {
                    temp = j - i + 1;
                    if (temp > result) 
                        result = temp;
                }        
    

      v[i] * p 会不会溢出成为负值呢?

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    proxmox新版本使用了lxc容器,导致以前的vzlist命令无法使用,于是自己写了一个脚本来获取所有半虚拟化主机的信息状态
    Linux 系统优化参数总结
    linux shell 远程执行命令
    wios设置证书登陆
    Eclipse安装tomcat插件
    Centos6.5-dnsmasq安装
    SSL工作原理
    ssh免密码登陆及其原理
    Linux查看后台任务,关闭后台任务
    Mybatis 中常用的java类型与jdbc类型
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12642557.html
Copyright © 2011-2022 走看看