【题目描述】
有n
个人在一个水龙头前排队接水,假如每个人接水的时间为Ti
,请编程找出这n
个人排队的一种顺序,使得n
个人的平均等待时间最小。
【输入】
共两行,第一行为n(1≤n≤1000)
;第二行分别表示第1
个人到第n
个人每人的接水时间T1,T2,…,Tn
,每个数据之间有1
个空格。
【输出】
有两行,第一行为一种排队顺序,即1
到n
的一种排列;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。
【输入样例】
10
56 12 1 99 1000 234 33 55 99 812
【输出样例】
3 2 7 8 1 4 9 6 10 5
291.90
【思路】纯贪心,直接莽,不过得注意点时间,不然直接超时
#include<bits/stdc++.h>
using namespace std;
int n,sum=0;
double ans;
struct node{int id,t;
};
struct node t[1010];
bool cmp(node x,node y){return x.t<y.t;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){cin>>t[i].t ;t[i].id=i;
}
sort(t+1,t+n+1,cmp);
int j=n-1;
for(int i=1;i<=n;i++){ans=ans+j*t[i].t ;cout<<t[i].id<<" ";j--;
}
cout<<endl<<fixed<<setprecision(2)<<ans/n;return 0;
}