Maximum array distance Sum

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
内容概要:文章基于4A架构(业务架构、应用架构、数据架构、技术架构),对SAP的成本中心和利润中心进行了详细对比分析。业务架构上,成本中心是成本控制的责任单元,负责成本归集与控制,而利润中心是利润创造的独立实体,负责收入、成本和利润的核算。应用架构方面,两者都依托于SAP的CO模块,但功能有所区分,如成本中心侧重于成本要素归集和预算管理,利润中心则关注内部交易核算和获利能力分析。数据架构中,成本中心与利润中心存在多对一的关系,交易数据通过成本归集、分摊和利润计算流程联动。技术架构依赖SAP S/4HANA的内存计算和ABAP技术,支持实时核算与跨系统集成。总结来看,成本中心和利润中心在4A架构下相互关联,共同为企业提供精细化管理和决策支持。 适合人群:从事企业财务管理、成本控制或利润核算的专业人员,以及对SAP系统有一定了解的企业信息化管理人员。 使用场景及目标:①帮助企业理解成本中心和利润中心在4A架构下的运作机制;②指导企业在实施SAP系统时合理配置成本中心和利润中心,优化业务流程;③提升企业对成本和利润的精细化管理水平,支持业务决策。 其他说明:文章不仅阐述了理论概念,还提供了具体的应用场景和技术实现方式,有助于读者全面理解并应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值