排队接水
题目描述
有 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 1≤n≤1000, 1 ≤ t i ≤ 1 0 6 1\le t_i \leq 10^6 1≤ti≤106,不保证 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;
}