洛谷:P1223 排队接水

排队接水

题目描述

n n n 个人在一个水龙头前排队接水,假如每个人接水的时间为 T i T_i Ti,请编程找出这 n n n 个人排队的一种顺序,使得 n n n 个人的平均等待时间最小。

输入格式

第一行为一个整数 n n n

第二行 n n n 个整数,第 i i i 个整数 T i T_i Ti 表示第 i i i 个人的接水时间 T i T_i Ti

输出格式

输出文件有两行,第一行为一种平均时间最短的排队顺序;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。

样例 #1

样例输入 #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

提示

1 ≤ n ≤ 1000 1\le n \leq 1000 1n1000 1 ≤ t i ≤ 1 0 6 1\le t_i \leq 10^6 1ti106,不保证 t i t_i ti 不重复。

贪心问题

只需从小到大排列即可,根据题目范围,可将输入数据*1024+i便可记录它的位置,然后取模输出即可
//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>

using namespace std;

#define endl '\n'
const int M=1e6+7;


int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);

int n;cin>>n;vector<int> a(n);
for(int i=0;i<n;i++)
   {
   int b;
       cin>>b;
     a[i]=b*1024+i+1;
   }
   sort(a.begin(),a.end());
   double d;long long  ant=0;
   for(int i=0,k=n-1;i<n;i++,k--)
      {
            cout<<a[i]%1024<<' ';
          ant+=a[i]/1024*k;
          
 }
 d=1.0*ant/n;
 cout<<endl<<fixed<<setprecision(2)<<d;
return 0;
}

  • 12
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值