当前位置: 首页 > news >正文

Business Magic

题目描述

There are n stores located along a street, numbered from 1 to n from nearest to farthest. Last month, the storek had a net profit of rk . If rk is positive, it represents a profit of rk dollars; if rk is negative, it represents a loss of −rk dollars.
As a master of business magic, you have two types of spells at your disposal that you can use to alter the net profits of these stores for the next month:

1. Blue Magic: You can choose a single continuous interval [L, R] . The effect of this spell will be to double the net profit of every store from store L to store R (inclusive) for the next month. That is, if k ∈ [L, R] , then storek will have net profit 2rk next month.
2. Green Magic: You can choose any store and cast the green magic on it. The effect of the green magic is to change the next month’s net profit of that store to the negative of its last month’s net profit.

Any store that has not been affected by either spell will have the same net profit next month as it did last month.
However, there are some restrictions when casting spells. You can only cast the blue magic once and it must be used before the green magic. Additionally, the green magic cannot be cast on any store that has already been affected by the blue magic. Your task is to determine the maximum possible sum of the net profits for all stores for the next month after casting your spells optimally.

输入

The first line contains an integern , the numberof stores. The second line contains n space-separated integers r1 , r2 , . . . , rn , where rk is the net profit of store k last month.

输出

Output a single integer, the maximum possible total net profit of all stores forthe next month aftercasting the spells optimally.

样例输入
【样例1】
5
-2 5 -3 4 -1
【样例2】
7
-1 -1 -1 -1 -1 -1 -1
【样例3】
4
998244353 864197532 -7 1000000000
样例输出
【样例1】
20
【样例2】
7
【样例3】
5724883756
提示

Technical Specification
• 1 ≤ n ≤ 3 × 10^5
-10^9 ≤ rk ≤ 10^9 for k ∈ { 1, 2, . . . , n }

思路分析

prefix存储前缀和。

negat存储绝对值前缀和。

将最终结果ans初始化为negat[n]。(此为仅仅使用Green Magic的情况)

然后遍历R,寻找区间[L,R],更新ans。

当使用Blue Magic,得到的结果为\sum_{i=1}^{L-1}\left | r_i \right |+\sum_{i=R+1}^{n}\left | r_i \right |+2\times\sum_{i=L}^{R}r_i,整理之后得到\sum_{i=R+1}^{n}\left | r_i \right |+2\times\sum_{i=1}^{R}r_i-(2\times\sum_{i=1}^{L-1}r_i-\sum_{i=1}^{L-1}\left | r_i \right |)

在遍历R从1到n的过程中,顺便查找最小的2\times\sum_{i=1}^{L-1}r_i-\sum_{i=1}^{L-1}\left | r_i \right |,记为min_left,更新min_left,更新ans。

代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll ans;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>n;vector<int>r(n+1);vector<ll>prefix(n+1,0);vector<ll>negat(n+1,0);for(int i=1;i<=n;i++){cin>>r[i];prefix[i]=prefix[i-1]+r[i];negat[i]=negat[i-1]+abs(r[i]);}ans=negat[n];ll min_left=LONG_MAX;for(int i=1;i<=n;i++){min_left=min(min_left,2*prefix[i-1]-negat[i-1]);ans=max(ans,prefix[i]*2+negat[n]-negat[i]-min_left);}cout<<ans;return 0;
}
http://www.dtcms.com/a/334491.html

相关文章:

  • [创业之路-550]:公司半年度经营分析会 - 解决方案汇总
  • 【Java web】Servlet 详解
  • Linux -- 文件【下】
  • MATLAB R2010b系统环境(二)MATLAB环境的准备
  • 基于Transformer的机器翻译——模型篇
  • 力扣面试150(57/100)
  • 罗技MX Anywhere 2S鼠标修复记录
  • RocketMq面试集合
  • Redis--day6--黑马点评--商户查询缓存
  • 极简工具箱:安卓工具箱合集
  • redis的key过期删除策略和内存淘汰机制
  • Python爬虫实战:研究pygalmesh,构建Thingiverse平台三维网格数据处理系统
  • 记录Linux的指令学习
  • ktg-mes 改造成 Saas 系统
  • 后量子密码算法ML-DSA介绍及开源代码实现
  • 343整数拆分
  • 实例分割-动手学计算机视觉13
  • MQ积压如何处理
  • ABAP AMDP 是一项什么技术?
  • 深入理解Java虚拟机(JVM):架构、内存管理与性能调优
  • MongoDB 聚合提速 3 招:$lookup 管道、部分索引、时间序列集合(含可复现实验与 explain 统计)
  • 片料矫平机·第四篇
  • Element Plus 中 el-input 限制为数值输入的方法
  • 暴雨服务器:以定制化满足算力需求多样化
  • 深入剖析跳表:高效搜索的动态数据结构
  • 【测试工具】OnDo SIP Server--轻松搭建一个语音通话服务器
  • 社保、医保、个税、公积金纵向横向合并 python3
  • 深入理解 Vue Router
  • Centos7.9安装Dante
  • 04时间复杂度计算方法