Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is also an O(n) algorithm. Using the same observations as in a previous answer:</p> <blockquote> <p>Now consider what happens to the column sums when you rotate the second row to the left (e.g. change it from 1,2,...,N to 2,3,...,N,1): Each column sum will increase by 1, except for one column sum which is decreased by N-1.</p> <p>Instead of modifying all the column sums, we can decrease the one column sum by N, and then take the maximum column sum plus 1 to find the new maximum of the column sums. So we only need to update one column instead of all of them.</p> </blockquote> <p>The column where the maximum appears can only move to the left or jump back to the column with the overall maximum as we iterate over the possibilities of the second row. Candidate columns are the ones that were the temporary maximum in a left to right maximum scan.</p> <ol> <li>calculate all the sums for the first choice of the second row (1, 2, ..., N) and store them in an array.</li> <li>find the maximum in this array in a left to right scan and remember the positions of temporary maxima.</li> <li>in a right to left pass the sums are now decreased by N. If this decreasing process reaches the max column, check if the number is smaller than the overall maximum - N, in this case the new max column is the overall maximum column and it will stay there for the rest of the loop. If the number is still larger than the previous maximum determined in step 2, the max column will stay the same for the rest of the loop. Otherwise, the previous maximum becomes the new max column.</li> </ol> <p>Taking the example input 7,1,6,2 the algorithm runs as follows: Step 1 calculates the sum 8,3,9,6 Step 2 finds the temporary maxima from left to right: 8 in col 1 and then 9 in col 3 Step 3 generates the results passing over the array from right to left</p> <pre><code>8 3 9 6 -&gt; output 9 + 0 = 9 8 3 9 2 -&gt; output 9 + 1 = 10 8 3 5 2 -&gt; current max col is decreased, previous max 8 is larger and becomes current output 8 + 2 = 10 8 -1 5 2 -&gt; output 8 + 3 = 11 </code></pre> <p>Here is the algorithm in C:</p> <pre><code>#include &lt;stdio.h&gt; int N; int A[200000]; int M[200000]; int main(){ int i,m,max,j,mval,mmax; scanf("%d",&amp;N); for(i = 0;i &lt; N; i++){ scanf("%d",&amp;A[i]); A[i] = A[i]+i+1; } m = 0; max = 0; M[0] = 0; for(i = 1;i &lt; N; i++){ if(A[i] &gt; A[max]){ m++; M[m] = i; max = i; } } mval = A[max] - N; mmax = max; for(i = N-1,j = 0;i &gt;=0;i --,j++){ printf("%d ", A[max]+j); A[i] = A[i] - N; if(i == max){ if (A[i] &lt; mval) { max = mmax; } else if(m &gt; 0 &amp;&amp; A[i] &lt; A[M[m-1]]){ max = M[m-1]; m--; } } } printf("\n"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload