Codeforces 33C. Wonderful Randomized Sum(贪心)

本文介绍了一道 CodeForces 上的算法题,通过贪心策略实现最优操作以获得序列的最大和。解析了题目的核心思想,并给出了详细的 C++ 实现代码。

题目链接:http://codeforces.com/contest/33

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?

Input

The 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.

Output

The first and the only line of the output should contain the answer to the problem.

Examples
input
Copy
3
-1 -2 -3
output
Copy
6
input
Copy
5
-4 2 0 5 0
output
Copy
11
input
Copy
5
-1 10 -5 10 -2
output
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("in.txt","r",stdin);
    //freopen("out.txt","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;
        }
    }
    //得出ans
    int ans=nextmaxx[1];
    for(int i=1;i<n;i++)
        ans=max(ans,premaxx[i]+nextmaxx[i+1]);
    ans=max(ans,premaxx[n]);
    printf("%d\n",ans);
    return 0;
}

基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值