传送门

Description

给你一个数列\(A\),你可以选择任意一个前缀和任意一个后缀,前缀后缀可重合。给他们乘\(-1\)。求最大能获得的序列和。

Input

第一行是一个数\(n\)代表数列长度

第二行\(n\)个数字,代表序列的元素

Output

一个数字代表答案

Hint

\(1~\leq~n~\leq~10^5\)

Solution

发现重合的部分相当于没有修改。于是不妨设前后缀没有重合。

设修改的原序列前缀和为\(A\),后缀和为\(B\),未修改的部分为\(C\),序列和为\(S\),则有\(A+B+C=S\)。

题目要求最大化\(-(A+B)+C\)。又因为\(A+B=S-C\)。

于是要最大化\(2C-S\)。发现\(S\)是一个不变量,于是要最大化\(C\)。即求一个最大字段和。显然可以DP求解。于是本题可以解决了。

Code

#include<cstdio>
#define rg register
#define ci const int
#define cl const long long typedef long long int ll; template <typename T>
inline void qr(T &x) {
rg char ch=getchar(),lst=' ';
while((ch > '9') || (ch < '0')) lst=ch,ch=getchar();
while((ch >= '0') && (ch <= '9')) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
if(lst == '-') x=-x;
} namespace IO {
char buf[120];
} template <typename T>
inline void qw(T x,const char aft,const bool pt) {
if(x < 0) {x=-x,putchar('-');}
rg int top=0;
do {IO::buf[++top]=x%10+'0';} while(x/=10);
while(top) putchar(IO::buf[top--]);
if(pt) putchar(aft);
} template <typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template <typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template <typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;} template <typename T>
inline void mswap(T &_a,T &_b) {
T _temp=_a;_a=_b;_b=_temp;
} const int maxn = 100010; int n,ans,sum;
int MU[maxn],frog[maxn]; int main() {
qr(n);
for(rg int i=1;i<=n;++i) qr(MU[i]);
for(rg int i=1;i<=n;++i) {
frog[i]=mmax(frog[i],frog[i-1]+MU[i]);
ans=mmax(ans,frog[i]);
sum+=MU[i];
}
qw((ans<<1)-sum,'\n',true);
return 0;
}

Summary

遇到最大化一个值的题目,可以通过数学推导变成单变量极值问题,然后DP或者数学求解。

【极值问题】【CF33C】 Wonderful Randomized Sum的更多相关文章

  1. CF33C Wonderful Randomized Sum 题解

    原题链接 简要题意: 你可以无限次的把该数组的一个前缀和后缀 \(\times -1\),问最终的最大序列和. 这题盲目WA了数次才知道本质 这题89个数据吊打std CF真好啊,发现一个错后面就不测 ...

  2. codeforces每日一题1-10

    目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...

  3. E - Rebuild UVALive - 7187 (二次函数极值问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5531 Problem Description Archaeologists find ruins of ...

  4. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  9. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. html5shiv 是一个针对 IE 浏览器的 HTML5 JavaScript 补丁,目的是让 IE 识别并支持 HTML5 元素。

    html5shiv 是一个针对 IE 浏览器的 HTML5 JavaScript 补丁,目的是让 IE 识别并支持 HTML5 元素. 各版本html5shiv.js CDN网址:https://ww ...

  2. 【python 3.6】python获取当前时间及过去或将来的指定时间

    最近有个查询api,入参需要一个startTime,一个endTime,刚好用到datetime. 留此记录. #python 3.6 #!/usr/bin/env python # -*- codi ...

  3. 3.10-通过requests、BeautifulSoup、webbrowser模块的相关方法,爬取网页数据示例程序(一)

    import requests,bs4res=requests.get('https://www.hao123.com/')print('res对象的类型:',type(res))res.raise_ ...

  4. 剑指 Offer——数字在排序数组中出现的次数

    1. 题目 2. 解答 时间复杂度为 \(O(n)\) 的算法,顺序遍历数组,当该数字第一次出现时开始记录次数. class Solution { public: int GetNumberOfK(v ...

  5. PPM、PGM、PBM图像格式剖析

    今天突然需要用到PPM这个图像文件格式,之前没见过,在此记录一下. PPM.PGM.PBM这三个图像文件格式很少见,其实也不难,分别用于彩色图像.灰度图像.二值图像.这里以PPM格式为例. PPM格式 ...

  6. 算法笔记(c++)--c++中碰到的一些用法

    算法笔记(c++)--c++中碰到的一些用法 toupper(xxx)可以变成大写; tolower(xx)小写 isalpha(xxx)判断是不是字母 isalnum(xx)判断是不是数字 abs( ...

  7. Markdown分级语法手册

    目录 前言(可以不看) 基本语法(18) 1. 标题:# 2. 无序列表:- 3. 有序列表:1. 4. 斜体:* 5. 粗体:** 6. 加粗斜体:*** 7. 删除线:~~ 8. 分隔线:--- ...

  8. Python 装饰器Decorator(一)

    (一) 装饰器基础知识 什么是Python装饰器?Python里装饰器是一个可调用的对象(函数),其参数是另一个函数(被装饰的函数) 假如有一个名字为somedecorator的装饰器,target是 ...

  9. MD5加密字符串--基于python

    import hashlib#md5加密32位def md5(str): import hashlib m = hashlib.md5() m.update(str) return m.hexdige ...

  10. MySQL case when 使用

    case when 自定义排序时的使用 根据 case when 新的 sort字段排序 case when t2.status = 4 and t2.expire_time>UNIX_TIME ...