穷举 24 种顺序最小是多少
四个任务一共有 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 编译。)
全部评论