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.pswp.cn/pingmian/93282.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/93282.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/93282.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在ubuntu系統上離線安裝jenkins的做法

作者&#xff1a;朱金燦 來源&#xff1a;clever101的專欄 1.安裝java環境和下載war包&#xff1a; Jenkins 依賴于 Java 環境&#xff08;OpenJDK 11 或更高版本&#xff09;&#xff1a; # 安裝OpenJDK 11和字體依賴 sudo dpkg -i openjdk-11-jre-headless_*.deb fontconfi…

圖像相似度算法匯總及Python實現

下面整理了一些圖像相似度算法&#xff0c;可根據不同的需求選擇不同的算法&#xff0c;對每種算法進行了簡單描述并給出Python實現&#xff1a; 1. 基于像素的算法&#xff1a; (1).MSE(Mean Squared Error)&#xff1a;均方誤差&#xff0c;通過計算兩幅圖像對應像素值差的平…

IO流與單例模式

單例模式 單例模式是指一個類只能有一個對象。 餓漢模式 在單例模式下&#xff0c;在程序開始&#xff08;main函數運行前&#xff09;的時候創建一個對象&#xff0c;這之后就不能再創建這個對象。 class HungryMan { public:static HungryMan* getinstance(){return &ins…

Java設計模式之依賴倒置原則使用舉例說明

示例1&#xff1a;司機駕駛汽車 問題場景&#xff1a;司機類直接依賴奔馳車類&#xff0c;新增寶馬車需修改司機類代碼。 // 未遵循DIP class Benz { public void run() { /*...*/ } } class Driver { public void drive(Benz benz) { benz.run(); } } // 遵循DIP&#xff1a;…

【Docker】openEuler 使用docker-compose部署gitlab-ce

docker-compose配置 services:gitlab:image: gitlab/gitlab-ce:latestcontainer_name: gitlabrestart: alwayshostname: gitlab.example.comenvironment:GITLAB_OMNIBUS_CONFIG: |# Add any other gitlab.rb configuration here, each on its own lineexternal_url https://gi…

ElasticSearch 父子文檔使用簡記

一. ES parent-child 文檔簡介 ES 提供了類似數據庫中 Join 聯結的實現&#xff0c;可以通過 Join 類型的字段維護父子關系的數據&#xff0c;其父文檔和子文檔可以單獨維護。 二. 父子文檔的索引創建與數據插入 ES 父子文檔的創建可以分為下面三步&#xff1a; 創建索引 M…

【Linux】編輯器vim的使用

目錄 1. vim的基本概念 2. vim的基本使用 3. vim命令模式操作 3.1 移動光標 3.2 刪除 3.3 復制 3.4 替換 3.5 撤銷 3.6 更改 3.7 跳轉 4. vim底行模式操作 4.1 列出行號 4.2 跳到文件中的某行 4.3 查找字符 4.4 保存文件 4.5 離開vim 1. vim的基本概念 Vim&…

《零基礎掌握飛算Java AI:核心概念與案例解析》

前引&#xff1a;飛算科技是一家專注于企業級智能化技術服務的公司&#xff0c;核心領域包括AI、大數據、云計算等。其Java AI解決方案主要面向企業級應用開發&#xff0c;提供從數據處理到模型部署的全流程支持&#xff01;飛算Java AI是一款基于人工智能技術的Java開發輔助工…

Chrome騰訊翻譯插件transmart的安裝

文章目錄一、官網地址二、安裝過程1. 下載插件2. 解壓crx3, chrome安裝三、如何使用一、官網地址 騰訊翻譯插件官網 二、安裝過程 1. 下載插件 點擊上面的官網地址&#xff0c;下拉到如圖所示chrome插件位置&#xff0c;點擊立即下載 2. 解壓crx 從壓縮文件中解壓出crx文…

IOMMU的2級地址翻譯機制及多級(2~5)頁表查找

IOMMU的2級地址翻譯機制及多級(2~5)頁表查找 摘要:IOMMU是現代計算機系統中用于I/O設備(如GPU、NIC、網絡接口卡)的地址翻譯和保護機制,類似于CPU的MMU(Memory Management Unit),但專為設備DMA(Direct Memory Access,直接內存訪問)設計。它支持虛擬化環境(…

C++STL標準模板庫詳解

一、引言STL&#xff08;Standard Template Library&#xff09;是 C 標準庫的核心組成部分&#xff0c;其中容器&#xff08;Containers&#xff09; 作為數據存儲的基礎組件&#xff0c;為開發者提供了豐富的數據結構選擇。本文將聚焦 STL 容器的核心類型&#xff0c;結合具體…

神經網絡 常見分類

&#x1f4da; 神經網絡的常見分類方式可以從不同角度來劃分&#xff0c;以下是幾種主流思路&#xff0c;幫你快速梳理清晰&#xff1a;1?? 按網絡結構分類前饋神經網絡&#xff08;Feedforward Neural Network, FNN&#xff09; 數據從輸入層→隱藏層→輸出層單向傳遞&#…

生產環境Redis緩存穿透與雪崩防護性能優化實戰指南

生產環境Redis緩存穿透與雪崩防護性能優化實戰指南 在當下高并發場景下&#xff0c;Redis 作為主流緩存組件&#xff0c;能夠極大地提升讀寫性能&#xff0c;但同時也容易引發緩存穿透、緩存擊穿及緩存雪崩等問題&#xff0c;導致后端依賴數據庫的請求激增&#xff0c;系統穩定…

【洛谷刷題】用C語言和C++做一些入門題,練習洛谷IDE模式:分支機構(一)

&#x1f525;個人主頁&#xff1a;艾莉絲努力練劍 ?專欄傳送門&#xff1a;《C語言》、《數據結構與算法》、C語言刷題12天IO強訓、LeetCode代碼強化刷題、洛谷刷題、C/C基礎知識知識強化補充、C/C干貨分享&學習過程記錄 &#x1f349;學習方向&#xff1a;C/C方向 ??人…

嵌入式硬件篇---常見的單片機型號

以下是目前常用的單片機型號及其應用場景、優劣勢的詳細解析&#xff0c;結合最新行業動態和技術特性&#xff0c;幫助你精準匹配需求&#xff1a;一、經典 8 位單片機&#xff1a;低成本入門首選1. 51 系列&#xff08;代表型號&#xff1a;AT89C51、STC89C52&#xff09;應用…

windows下ArcGIS 10.8.2下載安裝教程

ArcGIS是由美國環境系統研究所&#xff08;Esri&#xff09;開發的一款功能強大且應用廣泛的綜合性地理信息系統&#xff08;GIS&#xff09;軟件平臺&#xff0c;在空間數據的采集、管理、分析、可視化和共享等方面表現出色&#xff0c;是GIS領域的標桿產品。它擁有豐富的功能…

防御保護15

混合密碼體系 --- 數字信封 邏輯 --- 先用快速的對稱密鑰來對消息進行加密&#xff0c;保證數據的機密性。然后只需要保證對稱密鑰的機密性即可&#xff0c;使用公鑰密鑰體系來對對稱秘鑰消息進行加密。身份認證和數據認證技術 Hash散列 指紋 ---> 單向散列函數 Hash --->…

Linux上管理Java的JDK版本

1.alternatives簡介alternatives是 Linux 系統&#xff08;尤其是 ??RHEL/CentOS/Fedora?? 等基于 RPM 的發行版&#xff09;中用于管理??同一軟件多個版本??的系統工具。它通過維護符號鏈接&#xff08;軟鏈接&#xff09;的層級結構&#xff0c;幫助用戶在不沖突的情…

webrtc編譯arm/arm64

webrtc版本 m125版本 編譯arm sudo apt install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf //下載失敗,需要多次嘗試 python3 build/linux/sysroot_scripts/install-sysroot.py --arch=arm //python3 bui

【讀論文】醫療AI大模型:百川開源Baichuan-M2

1. 引言 最新百川開源了一個可以和openai新模型掰手腕的醫療垂直大模型:Baichuan-M2在HealthBench基準上取值60.1的高分,超過了gpt-oss-120b。這次一起回顧下百川給的技術報告。 2. Baichuan-M2概覽:“模型+系統” Baichuan-M2的成功源于一套精心設計的、端到端的訓練與優…