Maximum array distance Sum

本文介绍了一种寻找数组中最大距离和的方法。该方法通过分析数组特性,利用两个遍历过程来确定最大值,其中一个从前往后遍历,另一个从后往前遍历。这种方法不依赖额外的大内存空间,并且适用于各种数组数据类型。

Question: Given an array, find the maximum array distance sum. Array distance sum is defined as below. For any 0 <= p <= q, the array distance is A[p]  + A[q] + (q - p)

For example, Given array {0, 2, -1}

A[0][0] = 0, A[0][1] = 3, A[0][2] = 1; A[1][1] = 2 + 2 + (1 - 1) = 4; A[1][2] = 2 + -1 + (2 - 1) = 2. A[2][2] = -1 + -1 + 0 = -2. Thus, the maximum array distance sum is: 4.

If allows extra O(N) memory space, we can do it by allocating an array for (A[p] - p), and another for (A[q] + q). Traverse the two array from front-end and end-front to get the maximum value.

If extra space was required for O(1), we need to analyse the question a bit. The array distance equals to A[p] - p + (A[q] + q). To get the max, we need to get max(A[p] - p) + max(A[q] + q),  We can get that the max(A[p] - p) is the peak value in the array. However, max(A[q] + q) might exist after the peak.  

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

int getMaxDistanceSum(vector<int>& array) {
    int forwardMax = INT_MIN;
    int backwardMax = INT_MIN;
    for(int i = 0; i < array.size(); ++i) {
       forwardMax = max(forwardMax, array[i] - i);
       backwardMax = max(backwardMax, array[i] + i);
    }
    return forwardMax + backwardMax;
}
% This is a comment line % This is SimulatedAnnealing.m file source code % The function with the same name as file name will be executed function SimulatedAnnealing() nCities = 100; initialTemperature = 100; endTemperature = 0; % setup initial cities positions as 2-dimentional array (x,y) with % maximum value of 10 cities = rand(nCities, 2)*10; figure % Create new canvas for plot % draw initial route. % cities(:, 1) - The colon (`:`) alone, without start or end values, specifies all of the elements in that dimension. plot(cities(:, 1), cities(:, 2), "b--o" ); title('Initial route') % call our optimization function state = OptimiseRoute(cities, initialTemperature, endTemperature); figure % draw final route plot(cities(state,1),cities(state,2),"r--o"); title('Optimized route') end function [ state ] = OptimiseRoute(cities, initialTemperature, endTemperature) nCities = size(cities,1); % setup initial cities visit order as numbered column-vector % ' - is the transpose operator state = (1:nCities)'; % calculate the energy for the initial condition, in our meaning = route length currentEnergy = CalculateEnergy(state, cities); disp('Initial route length: '); disp(currentEnergy); T = initialTemperature; for k = 1:100000 % main loop % create a new order for visiting cities stateCandidate = GenerateStateCandidate(state); % calculate new order energy (route length) candidateEnergy = CalculateEnergy(stateCandidate, cities); if(candidateEnergy < currentEnergy) % if the new order has less energy state = stateCandidate; % it became the new order currentEnergy = candidateEnergy; else % otherwise, calculate the probability of accepting bad solution p = GetTransitionProbability(candidateEnergy-currentEnergy, T); if (IsTransition(p)) % if the transition occurs with a given probability state = stateCandidate; % accept the new bad solution currentEnergy = candidateEnergy; end end T = DecreaseTemperature(initialTemperature, k); if(T <= endTemperature) % second exit condition break; end end disp('Final route length: '); disp(currentEnergy); end function [ E ] = CalculateEnergy(sequence, cities) % calculate route length n = size(sequence,1); % get size of first dimention (row count) E = 0; for i = 1:n-1 E = E + Metric(cities(sequence(i),:), cities(sequence(i+1),:)); end % add distance between finish and start to return to initial point E = E + Metric(cities(sequence(end),:), cities(sequence(1),:)); end function [ distance ] = Metric( A, B ) % calculate distance between 2 points distance = (A - B).^2; distance = sqrt(distance); distance = sum(distance); end function [ T ] = DecreaseTemperature( initialTemperature, k ) T = initialTemperature * 0.1 / k; end function [ P ] = GetTransitionProbability( dE, T ) P = exp(-dE/T); end function [ a ] = IsTransition( probability ) if(rand(1) <= probability) a = 1; else a = 0; end end function [ seq ] = GenerateStateCandidate(seq) n = size(seq, 1); % get size of cities indexes array i = randi(n); % get a pseudorandom index between 1 and n j = randi(n); % the same for the 2nd element % swap 2 points t = seq(i); seq(i) = seq(j); seq(j) = t; end 任务目标:改进TSP(旅行商问题)程序,将简单的swap操作替换为2-Opt Swap(路径反转),以优化路径结果。 要求:使用给定的算法和初始点生成设置,实现更短路径的搜索。
07-11
【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)内容概要:本文提出了一种基于融合鱼鹰和柯西变异的麻雀优化算法(OCSSA)优化变分模态分解(VMD)参数,并结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)的轴承故障诊断模型。该方法利用西储大学公开的轴承数据集进行验证,通过OCSSA算法优化VMD的分解层数K和惩罚因子α,有效提升信号分解精度,抑制模态混叠;随后利用CNN提取故障特征的空间信息,BiLSTM捕捉时间序列的动态特征,最终实现高精度的轴承故障分类。整个诊断流程充分结合了信号预处理、智能优化与深度学习的优势,显著提升了复杂工况下轴承故障诊断的准确性与鲁棒性。; 适合人群:具备一定信号处理、机器学习及MATLAB编程基础的研究生、科研人员及从事工业设备故障诊断的工程技术人员。; 使用场景及目标:①应用于旋转机械设备的智能运维与故障预警系统;②为轴承等关键部件的早期故障识别提供高精度诊断方案;③推动智能优化算法与深度学习在工业信号处理领域的融合研究。; 阅读建议:建议读者结合MATLAB代码实现,深入理解OCSSA优化机制、VMD参数选择策略以及CNN-BiLSTM网络结构的设计逻辑,通过复现实验掌握完整诊断流程,并可进一步尝试迁移至其他设备的故障诊断任务中进行验证与优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值