把一处逆序换过来
四个任务耗时 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 编译。)