把一处逆序换过来

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

四个任务耗时 4、1、3、2,前两个是一处逆序(大的排在小的前面)。把它俩换过来,看总等待时间的变化。

运行下面这段程序:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int wait_total(const vector<int>& order) {
    // 按给定顺序做任务:把每个任务的完成时刻加起来
    int t = 0;
    int s = 0;
    for (int x : order) {
        t += x;
        s += t;
    }
    return s;
}

int main() {
    cout << wait_total({4, 1, 3, 2}) << "/" << wait_total({1, 4, 3, 2}) << endl;
}

(本题用 g++ -std=c++17 -O0 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论
🔒 登录并作答后可看讨论