分而治之 - 买卖? (最大连续子阵列)

时间:2012-09-16 22:44:01

标签: c++ recursion divide-and-conquer

该程序的要点是找到一维数组的最大子数组,该数组使用强力方法(即工作!)和分而治之方法(不工作),使股票价格波动。该程序的目的是找到一组天(因此结构中的lsub和rsub)以及那些天的最大利润。

我在网上看到的所有地方,例如this powerpoint都显示我的代码应该有效。我也看到了类似的东西,但是在StackOverflow上的Java中,该代码的实现也不起作用。 Here is the link to that Java program.

我的代码如下:

#include <stdio.h>
#include <iostream>
#include <climits>
#include <cstring>

struct funOut{
int lsub; //Left subscript
int rsub; //Right subscript
int maximum; //Contains the max value
 //This way, max has the absolute smallest value and only needs to be done once rather than at the beginning of the algorithm calls.
};

void load(int arr[], int n);
void brute(int arr[],  int n, funOut &out);
funOut dac(int arr[], int low, int high, funOut &out);
void findMax(int sumLval, int sumRval, funOut &sumMval, int low, int mid, int high, funOut &out);
funOut crossSum(int arr[], int low, int mid, int high);
void print(funOut out);

using namespace std;


int main(void)
{
   funOut out;

   string alg;
   cin >> alg;

   int n;
   cin >> n;

   int arr[n];

   // cout << n << endl;

   load(arr, n);
   if(alg[0] == 'b')
      brute(arr, n, out); //PARAMETERS NEEDED
   else if(alg[0] == 'd')
   {
      out.maximum = 0;
      out = dac(arr, 1, n-1, out);
   }

   else
   {
      cout << "\nERROR: No algorithm chosen, aborting program." << endl;
      return 0;
   }

   cout << "Before Print" << endl;
   print(out);
   return 0;
}

void load(int arr[], int n)
{
   cout << "Loading" << endl;
   for(int i=0; i<n; i++)
      cin >> arr[i];
}

void brute(int arr[], int n, funOut &out) //THIS WORKS!!!
{
   out.maximum = 0;
   int change;
   int temp = 0;
   for(int i=1; i<n-1; i++)
   {
      for(int j=i; j<n; j++)
      {
         change = arr[j] - arr[j-1];
         temp += change;
         if(temp > out.maximum){
            out.lsub = i;
            out.rsub = j;
            out.maximum = temp;
         }     
      }
      temp = 0;
   }
}

funOut dac(int arr[], int low, int high, funOut &out)
{
   cout << "DAC Start!" << endl;
   if(low == high)
   {
      out.lsub = out.rsub = low;
      out.maximum = arr[low];
      return out;
   }
   else
   {
      // cout << "DAC IF" << endl;
      int mid = (low + high)/2;

      funOut sumLval = dac(arr, low, mid, out);
      funOut sumRval = dac(arr, mid+1,high, out);
      funOut sumMval = crossSum(arr, low, mid, high);

      cout << "\nsumLval = " << sumLval.maximum << endl; 
      cout << "\nsumRval = " << sumRval.maximum << endl;
      cout << "\nsumMval = " << sumMval.maximum << endl;
      //FindMax
      if(sumLval.maximum >= sumRval.maximum && sumLval.maximum >= sumMval.maximum)
         return sumLval;         
      else if(sumRval.maximum >= sumLval.maximum && sumRval.maximum >= sumMval.maximum)
         return sumRval;
      else
         return sumMval;
   }
}



funOut crossSum(int arr[], int low, int mid, int high)
{
funOut sumMval;
int lsum = 0;
int rsum = 0;
int sum = 0;
int maxl, maxr;
//For loop finding lsum
for(int i=mid; i>=low; i--)
{
   cout << "DAC For I = " << i << endl;
   sum += arr[i];
   if(sum > lsum)
   {
      lsum = sum;
      maxl = i;
   }
}

sum = 0;

for(int j=mid+1; j<=high; j++)
{
   cout << "DAC For J = "<< j << endl;
   sum += arr[j];
   if(sum > rsum)
   {
      rsum = sum;
      maxr = j;
   }
}

sumMval.lsub = maxl;
sumMval.rsub = maxr;
sumMval.maximum = lsum + rsum;

return sumMval;
}

void print(funOut out)
{
   cout << "The max value is: ";
   cout << out.maximum << endl;
   cout << "The left subscript is: ";
   cout << out.lsub << endl;
   cout << "The right subscript is: ";
   cout << out.rsub << endl;
}

示例数据集:(它们应该在单独的行上但不允许我这样做。)

d
17
100
113
110
85
105
102
86
63
81
101
94
106
101
79
94
90
97

预期输出为:

最大值为:43

左下标是:8

右下标是:11

1 个答案:

答案 0 :(得分:0)

从概念上讲,你的暴力方法正在做大量的多余工作。

当你将数组中的所有点对点差异相加时,你得到的就是两端之间的差异:

int diff = (x[1] - x[0]) + (x[2] - x[1]) + (x[3] - x[2]) + (x[4] - x[3]);

以上内容与此相同(因为所有其他值都取消了):

int diff = x[4] - x[0];

因此,您所有的强力算法似乎都在搜索ij,其最大差异受i < j约束。这相当于找到数组的最小值和最大值,其中min出现在最大值

之前

在我看来,这是动态编程的一个问题。我不明白分而治之是如何对这个问题有用的。

<强> [编辑]

所以,我认为问题是crossSum。它正在做与brute截然不同的事情。当然,您只想在左侧(minl)找到最小值的索引,在右侧找到最大值的索引(maxr)。输出结构中的“最大值”只是arr[maxr] - arr[minl]

有趣的是,对于这个问题,分而治之的方法仍然是不必要的低效率。考虑一下:

struct funOut f;
f.lsub = 0;
f.rsub = 0;
f.maximum = 0;

int minval = arr[0];
int minidx = 0;

for( int i = 1; i < n; i++ ) {
    if( arr[i] < minval ) {
        minval = arr[i];
        minidx = i;
        continue;
    }

    int delta = arr[i] - minval;
    if( delta > f.maximum ) {
        f.lsub = minidx;
        f.rsub = i;
        f.maximum = delta;
    }
}