【极值问题】【CF33C】 Wonderful Randomized Sum
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的更多相关文章
- CF33C Wonderful Randomized Sum 题解
原题链接 简要题意: 你可以无限次的把该数组的一个前缀和后缀 \(\times -1\),问最终的最大序列和. 这题盲目WA了数次才知道本质 这题89个数据吊打std CF真好啊,发现一个错后面就不测 ...
- codeforces每日一题1-10
目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...
- E - Rebuild UVALive - 7187 (二次函数极值问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5531 Problem Description Archaeologists find ruins of ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- Linux目录与文件操作
文件命名规则: 1.严格区分大小写: 2.长度不能超过255个字符: 3.不能使用/当文件名 mkdir:创建空目录 -p:parent,父目录,逐级创建 -v:verbose,打印详细信息 命令行展 ...
- [C++]C++得到最大的int值
要得到最大的int值: 利用(unsigned int)-1,这样得到的就是unsigned int表示的最大值, int值只是比unsigned int多一位符号位,所以对(unsigned int ...
- 英特尔近日发布最新版实感™ SDK R5 (v7)
英特尔开发人员专区 原文地址 英特尔® 实感™ SDK 的 7.0.23.8048 版本(也称为 R5)现已推出.您将看到的主要变化包括: 支持英特尔® 实感™ SR300 摄像头:应于 2016 年 ...
- 【bzm-Random CSV Data Set Config】 -jmeter - 文件中随机取参的方法,(插件自带)
文件中随机取参数的方法 Random CSV Data Set Config
- Hyperledger Fabric 1.0 从零开始(十三)——orderer分布式方案
简述 在搭建HyperLedger Fabric环境的过程中,我们会用到一个configtx.yaml文件(可参考Hyperledger Fabric 1.0 从零开始(八)——Fabric多节点集群 ...
- 【MySQL解惑笔记】Mysql5.7.x无法开启二进制日志
一.开启二进制日志 1)未开启二进制日志之前: mysql> show variables like 'log_bin'; +---------------+-------+ | Variabl ...
- Just a Hook:线段树+区间修改
E - Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most ...
- 腾讯视频qlv格式转换MP4普通视频方法
QLV格式视频不是那么好对付的,似乎是一种加密格式,试着把.qlv改成.mp4或.flv都没有用,用格式工厂等转换软件转换也根本无法识别.但这并不意味着没有办法,其实真正的方法是不用任何工具: 1,我 ...
- nginx原声方法按照每天日志切割保存
首先配置日志变量,然后配置日志 在/etc/nginx/conf.d/default.conf 配置变量 server{ if ($time_iso8601 ~ "^(\d{4})-(\d{ ...
- Java:有关自定数组的学习
Java:有关==自定数组==的学习 在 ==<Java程序设计与数据结构教程>== 里我在==P212~P213==页看到一个GradeRange的程序,它用的数组是自定设定的Grade ...