0

I am doing a problem in which there is a need to find sum of maximum elements in a segment - sum of minimum elements in a segment.I tried using Sparse Table ,but it is two slow for the time limit.So i did something like this:

If n=4 segments are [1,2],[1,3],[1,4],[2,3],[2,4],[3,4]. The problem is similar to an RMQ problem but i have to do it for all segments and find the

sum=max(a[1],a[2])+ max(a[1],a[2],a[3])+max(a[1],a[2],a[3],a[4])+max(a[2],a[3])+m‌​ax(a[2],a[3],a[4])+max(a[3],a[4])-min(a[1],a[2])+min(a[1],a[2],a[3])+min(a[1],a[2‌​],a[3],a[4])+min(a[2],a[3])+min(a[2],a[3],a[4])+min(a[3],a[4])

for(i=1;i<n;i++)
{
    maxtilli[i-1]=INT_MIN;
    mintilli[i-1]=INT_MAX;
    for(k=1,j=i;j<=n;k++,j++)
    {
        if(a[j]>maxtilli[k-1])
        {
            maxtilli[k]=a[j];
        }
        else
        {
             maxtilli[k]=maxtilli[k-1];
        }

        if(a[j]<mintilli[k-1])
        {
            mintilli[k]=a[j];
        }
        else
        {   
            mintilli[k]=mintilli[k-1];
        }
        if(i!=j)
        { 
            ans+=(maxtilli[k]-mintilli[k]);
        }
    }
}

Here n is of the order of 100,000. So is there any way to optimize it.

Suppose n=4 then segments are [1,2],[1,3],[1,4],[2,3],[2,4],[3,4].

The thing required is sum=max(a[1],a[2])+max(a[1],a[2],a[3])+max(a[1],a[2],a[3],a[4])+max(a[2],a[3])+m‌​ax(a[2],a[3],a[4])+max(a[3],a[4])-min(a[1],a[2])+min(a[1],a[2],a[3])+min(a[1],a[2‌​],a[3],a[4])+min(a[2],a[3])+min(a[2],a[3],a[4])+min(a[3],a[4])

kanz
  • 87
  • 7
  • 3
    Not clear what you want to do. What is segment? What are "maximum elements in a segment"? –  Dec 03 '12 at 11:37
  • Suppose n=4 then segments are [1,2],[1,3],[1,4],[2,3],[2,4],[3,4].The thing required is sum=max(a[1],a[2])+max(a[1],a[2],a[3])+max(a[1],a[2],a[3],a[4])+max(a[2],a[3])+max(a[2],a[3],a[4])+max(a[3],a[4])-min(a[1],a[2])+min(a[1],a[2],a[3])+min(a[1],a[2],a[3],a[4])+min(a[2],a[3])+min(a[2],a[3],a[4])+min(a[3],a[4]) – kanz Dec 03 '12 at 11:39
  • 1
    isn't "maximum elements in a segment" an oxymoron i.e. there can, by definition, only be one maximum element, the biggest? – JustMaximumPower Dec 03 '12 at 12:00
  • You could sort the elements, **O(n log(n))**, then compute the answer **O(n)**. Is that fast enough? I'm not sure you can do better. – Beta Dec 03 '12 at 12:29
  • sorting will destroy the original positions of array and it cant be done – kanz Dec 03 '12 at 13:15

1 Answers1

0

We can try to finish the first problem, sum of the max value in all segments.

Algorithm

First, you can find the max value a[i] in the whole sequence. All segments which contain a[i] would be considered. The answer plus A[i] * (i * (n - i)). And the problem is split into two small sequences [1, i - 1] and [i + 1, n], you can do it in the same way.

Code

void cal(int L, int R){
    max_index = find_max(L, R); // O(logN), using Sparse Table or Segment Tree
    int all_segments = (max_index - L + 1) * (R - max_index)
    ans += a[max_index] * all_segments;
    cal(L, max_index - 1);
    cal(max_index + 1, R);
}
// call max_index N times, so the total complexity is O(N * logN)
Jun HU
  • 3,176
  • 6
  • 18
  • 21