题目描述
有 n 个人在一个水龙头前排队接水,假如每个人接水的时间为 Ti,请编程找出这 n 个人排队的一种顺序,使得 n 个人的平均等待时间最小。
输入描述
第一行为一个整数 n。
第二行 n 个整数,第 i 个整数 Ti 表示第 i 个人的等待时间 Ti。
输出描述
输出文件有两行,第一行为一种平均时间最短的排队顺序;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。
输入输出样例
-
输入
10 56 12 1 99 1000 234 33 55 99 812
输出
3 2 7 8 1 4 9 6 10 5 291.90
- 这个代码有些oj可以ac,有的不行,不行的话去试试C++排队接水-CSDN博客
-
#include<bits/stdc++.h> using namespace std; struct p{ int x,t; bool operator<(const p &w )const { return t<w.t; } }a[1000]; int main(){ int n; cin>>n; for(int i=0;i<n;i++){ a[i].x=i+1; cin>>a[i].t; } long long sum=0; sort(a,a+n); for(int i=0;i<n;i++){ cout<<a[i].x<<' '; //sum+=a[i].t; } for(int i=0;i<n-1;i++){ sum+=a[i].t*(n-i-1); } //cout<<sum<<endl; cout<<endl; cout<<fixed<<setprecision(2)<<sum*1.0/n; //cout<<"\a"; }//'\''