Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
88724 sh25_zhangyj [贪心算法]排队接水 C++ 通过 0 MS 260 KB 1217 2026-05-15 15:35:52

Tests(1/1):


#include <iostream> #include <vector> #include <algorithm> #include <iomanip> using namespace std; // 定义一个结构体,保存接水时间和原来的序号 struct Person { int time; int id; }; // 自定义排序规则:按接水时间升序排列 bool cmp(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].id = i + 1; // 序号从1开始 } // 按接水时间排序 sort(people.begin(), people.end(), cmp); // 输出排队顺序 for (int i = 0; i < n; ++i) { if (i > 0) cout << " "; cout << people[i].id; } cout << endl; // 计算总等待时间 long long total_wait = 0; long long current_sum = 0; for (int i = 0; i < n; ++i) { total_wait += current_sum; current_sum += people[i].time; } // 计算并输出平均等待时间,保留两位小数 double avg_wait = (double)total_wait / n; cout << fixed << setprecision(2) << avg_wait << endl; return 0; }


测评信息: