穷举 24 种顺序最小是多少

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

四个任务一共有 24 种排法,用 next_permutation 全部过一遍。

运行下面这段程序:

#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() {
    vector<int> p = {4, 1, 3, 2};
    sort(p.begin(), p.end());
    int m = 1000000000;
    do {
        m = min(m, wait_total(p));
    } while (next_permutation(p.begin(), p.end()));
    cout << m << endl;
}

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

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

                        
👩‍🏫
AI
💬 题目评论

全部评论