提交时间:2025-10-11 15:45:02
运行 ID: 69668
cpp 运行 #include <iostream> #include <vector> #include <algorithm> #include <iomanip> using namespace std; // 定义结构体存储接水时间和原始位置 struct Person { int time; int index; }; // 排序比较函数,按接水时间升序排列 bool compare(const Person &a, const Person &b) { return a.time < b.time; } int main() { int n; cin >> n; vector<Person> people(n); for (int i = 0; i < n; ++i) { cin >> people[i].time; people[i].index = i + 1; // 原始位置为1-based } // 按接水时间从小到大排序 sort(people.begin(), people.end(), compare); // 输出排序后的原始位置 for (int i = 0; i < n; ++i) { if (i > 0) { cout << " "; } cout << people[i].index; } cout << endl; // 计算总等待时间和平均等待时间 double total_wait = 0.0; int current_sum = 0; for (int i = 0; i < n - 1; ++i) { // 最后一个人无人等待,无需累加 current_sum += people[i].time; total_wait += current_sum; } double average = total_wait / n; // 输出平均等待时间,精确到小数点后两位 cout << fixed << setprecision(2) << average << endl; return 0; }