1 条题解

  • 0
    @ 2019-11-20 3:51:38

    【例58.2】 序列处理

    解题思路

    原始数据先求总和,再排序得到最大最小值;去重后逆序输出所有不同值。

    正确性说明

    按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。

    易错点

    总和按原始数组计算,不能先去重再求和。

    复杂度

    时间O(n log n),空间O(n)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        vector<long long> a(n);
        long long sum = 0;
        for (auto &x : a) {
            cin >> x;
            sum += x;
        }
        sort(a.begin(), a.end());
        cout << sum << "\n" << a.back() << "\n" << a.front() << "\n";
        a.erase(unique(a.begin(), a.end()), a.end());
        for (int i = int(a.size()) - 1; i >= 0; i--) {
            if (i != int(a.size()) - 1)
                cout << " ";
            cout << a[i];
        }
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    382
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者