zoukankan      html  css  js  c++  java
  • PAT 1085 Perfect Sequence[难]

    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 Mm×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 (105​​) 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 109​​.

    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<=m*p,M是最大值,m是最小值,从序列中找到尽可能多的数满足这个公式。

    //比如对给的样例:序列最大可以包含8个数,分别是 2 3 4 5 1 6 7 8就是这样。 但是最小也不一定是从序列最小值开始的,就是截取序列的一部分。

     这是我一开始写的,理解错题意了。还以为是最小值确定,找出集个最大值呢满足即可。牛客网上通过率为0,PAT上得了5分通过了3个测试点。

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int a[100001];
    int main() {
        int n,p,mn=1e9;
        cin>>n>>p;
        for(int i=0;i<n;i++){
            cin>>a[i];
            if(a[i]<mn)
                mn=a[i];
        }
        mn=p*mn;
        int ct=0;
        for(int i=0;i<n;i++){
            if(a[i]<=mn)
                ct++;
        }
        cout<<ct;
    
        return 0;
    }
    Wrong

    尝试用二分法写,依旧失败:

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int a[100001];
    int main() {
        int n,p;
        cin>>n>>p;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);//默认从小到大排序
        p*=a[0];
        int left=0,right=n-1,mid;
        while(left<=right){//最终结果是保存在mid里的。
            mid=(left+right)/2;
            if(a[mid]==p)break;
            if(p>a[mid])left=mid+1;
            else right=mid-1;
        }
        cout<<mid+1;
        return 0;
    }
    wrong2

    代码转自:https://www.nowcoder.com/questionTerminal/dd2befeb7b6e4cea856efc8aa8f0fc1c

    链接:https://www.nowcoder.com/questionTerminal/dd2befeb7b6e4cea856efc8aa8f0fc1c
    来源:牛客网
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        ios::sync_with_stdio(false);
        // 读入数据
        int N, p; cin >> N >> p;
        vector<int> data(N);
        for(int i=0; i<N; i++) {
            cin >> data[i];
        }
    
        // 处理数据
        sort(data.begin(), data.end());
        int maxNum = 0;
        for(int i=0; i<N; i++) {//这里是两层循环,
            while(i+maxNum<N && data[i+maxNum]<=data[i]*p) {
                maxNum++;
            }
        }
        cout << maxNum << endl;
        return 0;
    }

    //这个思路是真的厉害。

    1.计算maxNum,对每个i来说,如果长度小于那么肯定不会进入while循环,否则就可以++。

    2.真是太厉害了,学习了。

    代码来自:https://www.liuchuo.net/archives/1908

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

    //好难理解啊,我得再思考思考+1.

  • 相关阅读:
    腾讯微博
    城市左右选择添加按钮案例
    jQuery元素操作1
    动态创建表格
    五角星评论案例
    点击图片箭头回到顶部案例
    HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)
    HDU1165: Eddy's research II(递推)
    HDU1158:Employment Planning(线性dp)
    HDU1081:To The Max(最大子矩阵,线性DP)
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9781602.html
Copyright © 2011-2022 走看看