• 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)
  • 相关阅读:
    Spring IOC注入接口多实现解决
    Spring Security 学习总结
    Spring Boot自动配置与Spring 条件化配置
    1403. Minimum Subsequence in Non-Increasing Order
    1457. Pseudo-Palindromic Paths in a Binary Tree
    1368. Minimum Cost to Make at Least One Valid Path in a Grid
    1456. Maximum Number of Vowels in a Substring of Given Length
    1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
    1472. Design Browser History
    1471. The k Strongest Values in an Array
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12642557.html
Copyright © 2020-2023  润新知