优先队列,没什么好说的。
#include<iostream> #include<queue> #include<functional> #include<iomanip> using namespace std; //n is the people's num,and k is the num of room int n, k; priority_queue<double,vector<double>, greater<double> > input,ans; int main() { while (cin >> n >> k){ double in,sum=0; for (int i = 0; i < n; i++){ cin >> in; input.push(in); } for (int i = 0; i < k; i++){ in = input.top(); sum += in; ans.push(in); input.pop(); } while(!input.empty()){ in= ans.top()+input.top(); sum += in; input.pop(); ans.pop(); ans.push(in); } cout << fixed << setprecision(3) << sum / n << endl; } return 0; }
原文地址:http://blog.csdn.net/hujian_/article/details/44805149