HOJ排队打水F601题解

题目

链接

http://oi.hdoi.cn/training/2/problem/P1257

字面描述

描述

有 n 个人在一个水龙头前排队接水,假如每个人接水的时间为T
i

,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小。

输入描述

共两行,第一行为n(1≤n≤1000);

第二行分别表示第1个人到第n个人每人的接水时间T
1

,T
2

,…,T
n

,每个数据之间有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
532.00

提示

update:2021.05.12: 提示:排序如果打水时间相同,按编号从小到大。

代码实现

#include<bits/stdc++.h>
using namespace std;

const int maxn=1e3+10;
int n;
double cnt;
struct node{
	int x,op;
	inline void input(){
		scanf("%d",&x);
	}
}a[maxn];
inline bool cmp(node u,node v){
	if(u.x!=v.x)return u.x<v.x;
	return u.op<v.op;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		a[i].input();
		a[i].op=i;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++){
		printf("%d ",a[i].op);
		cnt=cnt+(n-i+1)*a[i].x;
	}
	cnt=cnt/(n*1.0);
	printf("\n%.2lf\n",cnt);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值