贪心:排序不等式

排队打水:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
typedef long long LL;

int n;
int t[N];//总时间

int main()
{
    scanf("%d",&n);
    for (int i = 0; i < n; i ++ )scanf("%d",&t[i]);
    
    sort(t, t+n);//从小到大排序

    LL res = 0;//答案
    
    for (int i = 0; i < n; i ++ )
    {
        res += t[i]*(n-1-i);//计算公式
    }
    printf("%lld\n",res);

    return 0;
}
相关推荐
wuyk5551 分钟前
4.树:一对多的层次数据结构
开发语言·数据结构·stm32·单片机
watersink10 分钟前
机器学习聚类算法
算法·机器学习·聚类
luj_17681 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
会周易的程序员2 小时前
aiDgePLC iec61131 虚拟机 完整使用文档
c++·物联网·架构·st·iec61131
小小龙学IT2 小时前
Boost.Beast 深度实战:基于 Asio 的开源 C++ HTTP/WebSocket 协议库
c++·websocket·http
饼饼学习空间智能3 小时前
家庭服务机器人训练数据怎么积累?仿真、真实采集与持续学习的技术路线分析
人工智能·算法·机器学习
不可求~3 小时前
C++ std::string_view 不是字符串:从悬空引用到安全用法
java·开发语言·c++
en.en..4 小时前
数据结构考点解析:逻辑闭环与存储循环辨析
数据结构
小小龙学IT4 小时前
C++ 正则表达式完全指南:从 std::regex 实战到 RE2 引擎原理(NFA/DFA/回溯陷阱)
c++·正则表达式