首页 > 编程语言 >算法学习笔记(53)——排序不等式

算法学习笔记(53)——排序不等式

时间:2023-01-11 18:59:29浏览次数:56  
标签:不等式 int res LL 53 times 排序

排序不等式

题目链接:AcWing 913. 排队打水

让最磨叽的人最后打水

img

如图所示,第一个同学被等了6次,第二个同学被等了5次,以此类推...

\[总时间 = t_1 \times (n-1) + t_2 \times (n-2) + t_3 \times (n-3) + \cdots \]

按照从小到大的顺序排队,总时间最小。

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100010;

int n;    // 总人数
int t[N]; // 每位同学的打水时间

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> t[i];
    
    sort(t, t + n);
    
    LL res = 0;
    for (int i = 0; i < n; i ++ ) res += t[i] * (n - i - 1);
    
    cout << res << endl;
    
    return 0;
}

标签:不等式,int,res,LL,53,times,排序
From: https://www.cnblogs.com/I-am-Sino/p/17044644.html

相关文章