【Coding】寒假每日一题Day.8. 超级胶水

题目来源

题目来自于AcWing平台:https://www.acwing.com/problem/content/2871/。
以blog的形式记录程序设计算法学习的过程,仅做学习记录之用。

题目描述 + 输入输出格式 + 范围

在这里插入图片描述

样例

在这里插入图片描述

思路

思路参考自闫总的视频题解。

与区间合并不同,此处的合并代价是重量的乘积。通过数学归纳法可以证明,无论如何合并,最后的代价都是相等的,因此可以直接写代码了。

看来基础还是不能落下啊,写完寒假每日一题,要开始复习闫总的算法基础课了:)。

Code

代码参考自闫总的题解。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;

ll sum = 0, res = 0;
int n;

int main()
{
   
    cin >> n;
    while(n --){
   
        int x;
        cin >> x;
        res += sum * x;
        sum += x;
    }
    cout << res;
    return 0;
}

相关推荐

  1. LeetCode 每日 Day1

    2024-01-30 20:14:02       53 阅读
  2. LeetCode 每日 Day 11||贪心

    2024-01-30 20:14:02       61 阅读
  3. LeetCode 每日 Day 47 - 50

    2024-01-30 20:14:02       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-30 20:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-30 20:14:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-30 20:14:02       82 阅读
  4. Python语言-面向对象

    2024-01-30 20:14:02       91 阅读

热门阅读

  1. 【Vue】Vue3.0样式隔离

    2024-01-30 20:14:02       70 阅读
  2. STM32串口IAP

    2024-01-30 20:14:02       58 阅读
  3. 利用qrcode.vue库生成二维码

    2024-01-30 20:14:02       55 阅读
  4. 浅谈MySQL3种日志

    2024-01-30 20:14:02       49 阅读
  5. 数据库(SQL)

    2024-01-30 20:14:02       46 阅读
  6. Dockerfile实践

    2024-01-30 20:14:02       42 阅读
  7. Redis

    Redis

    2024-01-30 20:14:02      54 阅读
  8. 207. 课程表

    2024-01-30 20:14:02       63 阅读