题目描述
有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小。
输入输出格式
输入格式:
输入文件共两行,第一行为n;第二行分别表示第1个人到第n个人每人的接水时间T1,T2,…,Tn,每个数据之间有1个空格。
输出格式:
输出文件有两行,第一行为一种排队顺序,即1到n的一种排列;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。
输入输出样例
输入样例#1: 复制
10 56 12 1 99 1000 234 33 55 99 812
输出样例#1: 复制
3 2 7 8 1 4 9 6 10 5 291.90
说明
n<=1000
ti<=1e6,不保证ti不重复
当ti重复时,按照输入顺序即可(sort是可以的)
AC:
#include <iostream>
#include <algorithm>
using namespace std;
int n;//n个人接水
struct people
{
int num;
int water_time;
int total_wait;
}pp[5002];
bool cmp(people a, people b)
{
if(a.water_time == b.water_time) return a.num < b.num;
else return a.water_time < b.water_time;
}
int sum = 0; //某一人的等待时间
double ans = 0; //总等待时间
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
pp[i].num = i;
scanf("%d", &pp[i].water_time);
}
sort(pp+1, pp+n+1, cmp);
for (int i = 1; i <= n; i++)
{
printf("%d ", pp[i].num);
pp[i].total_wait = sum;
sum += pp[i].water_time;
ans += pp[i].total_wait;
}
printf("\n");
printf("%.2f",ans/n);
return 0;
}