A - Maximum sum-线性动态规划

Submit Status

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.







#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

int a[50001],l[50001],r[50001];
//求[0,i]区间的最大字段和,从前向后扫描;
int gl[50001],gr[50001];
int main()
{
     int N;
     cin>>N;
     while(N--)
     {
        int n;
        cin>>n;
        if(n<2)
            break;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        l[0]=a[0];
        for(int i=1;i<n;i++)
        {
            if(l[i-1]<0)
                l[i]=a[i];
            else
                l[i]=l[i-1]+a[i];
        }
        gl[0]=l[0];
        for(int i=1;i<n;i++)
            gl[i]=max(l[i],gl[i-1]);
//求[0,n-1]区间的最大字段和,从后向前扫描;
        r[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--)
        {
            if(r[i+1]<0)
                r[i]=a[i];
            else
                r[i]=r[i+1]+a[i];
        }
        gr[n-1]=r[n-1];
        for(int i=n-2;i>=0;i--)
            gr[i]=max(r[i],gr[i+1]);

       int res=-100000000;
        for(int i=1;i<n;i++)
        {
            res=max(res,gl[i-1]+gr[i]);
        }

        printf("%d\n",res);

     }
     return 0;
}



粒子群算法(Particle Swarm Optimization, PSO)是一种模拟鸟群捕食行为的群体智能优化算法。它通过模拟鸟群的飞行行为来求解最优解。Python是一种广泛应用的编程语言,具有简单易学和强大的科学计算库。 线性规划是一种通过线性目标函数和线性约束条件,寻找最小或最大值的优化问题。在使用粒子群算法求解线性规划问题时,可以按照以下步骤进行: 1. 初始化粒子群:随机生成一群粒子的初始位置和速度。 2. 计算适应度函数:根据线性规划问题的目标函数和约束条件,计算每个粒子的适应度值。 3. 更新粒子速度和位置:根据粒子群算法的更新公式,更新每个粒子的速度和位置。 4. 更新最优解:比较并更新全局最优解和个体最优解。 5. 终止条件判断:根据预设的终止条件,判断是否满足停止迭代的条件。 6. 迭代更新:如果终止条件未满足,返回第3步继续迭代,直到满足终止条件。 在Python中,可以使用numpy库进行矩阵计算,通过调用优化算法库(如pyswarm、scipy等)提供的PSO函数实现粒子群算法求解线性规划问题。具体的实现代码如下: ```python import numpy as np from pyswarm import pso # 定义目标函数 def objective_function(x): return np.dot(c, x) # 定义约束条件 def constraint_function(x): return np.dot(A, x) - b # 定义适应度函数 def fitness_function(x): constraints = constraint_function(x) constraints = np.maximum(0, constraints) return objective_function(x) - np.sum(constraints) # 定义线性规划问题的目标函数系数矩阵c、约束条件矩阵A和向量b c = np.array([1, 2, 3]) A = np.array([[1, 2, 3], [-1, 0, 1]]) b = np.array([10, -1]) # 设置最优解的边界约束 lb = np.array([0, 0, 0]) ub = np.array([1, 1, 1]) # 使用PSO函数求解线性规划问题 best_solution, best_fitness = pso(fitness_function, lb, ub) print('最优解:', best_solution) print('最优解的目标函数值:', best_fitness) ``` 以上代码使用pyswarm库中的pso函数,通过调用目标函数、约束函数和适应度函数,利用粒子群算法求解线性规划问题。最终得到的最优解和最优目标函数值可以通过打印输出来查看。 通过以上步骤和代码,就可以使用Python中的粒子群算法求解线性规划问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值