题目链接:
Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?
InputThe first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai( - 104 ≤ ai ≤ 104) — the sequence itself.
OutputThe first and the only line of the output should contain the answer to the problem.
Examples input Copy3 -1 -2 -3output Copy
6input Copy
5 -4 2 0 5 0output Copy
11input Copy
5 -1 10 -5 10 -2output Copy
18
题目大意:给你长度为n的序列,可以在前缀一起*-1或后缀一起*-1,问可能得到的最大序列和。
题目思路:贪心,对于当前点i,记录前面最大值和后面最大值即可。最大值?怎样才能得到当前的最大值,(1).1~i全部*-1有可能最大,(2)前面的某一截*-1+中间未*-1的有可能最大,前面某一截*-1,哪一截呢?就直接记录前面最大值(即是那某一截)+中间的和来更新。后缀最大一样的求法。最后for一遍更新一个ans即可。
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>using namespace std;#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;int a[N],b[N];
int presum[N],nextsum[N]; //记录*-1
int sum[N];
int premaxx[N],nextmaxx[N];int main()
{//freopen(","r",stdin);//freopen(","w",stdout);std::ios::sync_with_stdio(false);int n;cin>>n;for(int i=1;i<=n;i++)cin>>a[i],b[i]=-a[i];presum[0]=0;sum[0]=0;for(int i=1;i<=n;i++){sum[i]=sum[i-1]+a[i];presum[i]=presum[i-1]+b[i];}nextsum[n+1]=0;for(int i=n;i>=1;i--)nextsum[i]=nextsum[i+1]+b[i];//求前缀最大int tidx,maxx;if(presum[1]>a[1]){maxx=presum[1];tidx=1;premaxx[1]=maxx;}else{maxx=0;tidx=0;premaxx[1]=a[1];}for(int i=2;i<=n;i++){if(maxx+(sum[i]-sum[tidx])>maxx) //判断第(2)中情况,即某一截*-1{premaxx[i]=maxx+(sum[i]-sum[tidx]);}if(presum[i]>maxx) //前面一截全部*-1{tidx = i;maxx = presum[i];premaxx[i] = maxx;}}//求后缀最大if(nextsum[n]>a[n]){maxx=nextsum[n];tidx=n;nextmaxx[n]=maxx;}else{maxx=0;tidx=n+1;nextmaxx[n]=a[n];}for(int i=n-1;i>=1;i--){if(maxx+(sum[tidx-1]-sum[i-1])>maxx) //判断第(2)中情况,即某一截*-1{ nextmaxx[i]=maxx+(sum[tidx-1]-sum[i-1]);}if(nextsum[i]>maxx) //后面一截全部*-1{tidx = i;maxx = nextsum[i];nextmaxx[i] = maxx;}}//得出ansint ans=nextmaxx[1];for(int i=1;i<n;i++)ans=max(ans,premaxx[i]+nextmaxx[i+1]);ans=max(ans,premaxx[n]);printf("%dn",ans);return 0;
}
本文发布于:2024-01-28 22:42:42,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170645296710822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |