牛刀小试:程序设计能力实训 1041

Fifnmar edited 4 年,1 月前

我在这道简单题中应用了一些标准库设施。

其中,inner_product 本来是 transfrom_reduce,但我们 OJ 的 C++17 竟然没有!它支持不完全可还行。所以我换了。

template <typename F> inline void repeat(uint32_t const times, F func) {
    for (uint32_t i = 0; i < times; ++i) {
        func();
    } // 不知道为什么,如果写成 while(times--),那么 -O3 下的汇编指令会多出一条比较。
}

int main() {
    using namespace std;
    uint32_t n, m;
    cin >> n >> m;
    vector<uint32_t> storage(n + 1);
    for_each(next(begin(storage)), end(storage), [](auto &i) { cin >> i; });
    vector<uint32_t> price(n + 1, 0);
    repeat(m, [&price]() mutable {
        uint32_t p, q;
        cin >> p >> q;
        if (price[p] < q) {
            price[p] = q;
        }
    });
    cout << inner_product(next(begin(storage)), end(storage), next(begin(price)), (uint32_t)0);
}

Comments

Fifnmar

而且,Clang 的 -O3 好像不会做循环展开。