排队打水

题目描述

有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是可以的)

****贪心,注意啊ans定义的时候一定要定义成double

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 int i,j,n,s = 0;
 7 double ans = 0;
 8 struct node
 9 {
10     int x;
11     int id;
12 }a[1005];
13 int cmp(node c,node b)
14 {
15     return c.x < b.x;
16 } 
17 int main()
18 {
19     scanf("%d",&n);
20     for(i = 1;i <= n;i++)
21     {
22         scanf("%d",&a[i].x);
23         a[i].id = i; 
24     }    
25     sort(a + 1,a + 1 + n,cmp);
26     for(i = 1;i <= n;i++)
27     {
28     
29         printf("%d",a[i].id);
30         if(i != n)
31         printf(" ");
32         if(i != 1)
33         s += a[i - 1].x;
34         ans += s; 
35      } 
36     printf("
%0.2lf",(double)ans / n);
37     return 0;
38 }
原文地址:https://www.cnblogs.com/rax-/p/9844861.html