Divide the array into k contiguos partitions so that the sum of the maximum partition is minimal

Here, the maximum subset of the sum is one of k subsets that give the maximum sum, for example: arr = [10,5,3,7] and k = 2 possible ways to divide the prints into k subsets is {10, [5,3,7]} , {[[10,5], [3,7}, {[10,5,3], 7} and {[10,5], [3,7} is optimal. Edit: this is equivalent to https://www.codechef.com/DI15R080/problems/MINMAXTF

+2
source share
6 answers

Suppose you know that the answer is x, which means that the sum of the maximum subset is x. You can test this assumption by the greedy O (n) algorithm. (Move the array from left to right and select the elements until the sum of this subset drops below x). Now you can perform a binary search on x and find the minimum value for x. The complexity of this algorithm is O (nlogn).

+5
source

This is an example of a binary search in trial space.

int min_max_sum(std::vector<int> & a, int K) {        

    int n = a.size();    
    long long high = 0, low = 0, mid = 0;

    for (int i = 0; i < n; ++i) {
        high += a[i];
        low = max(a[i], low);
    }

    while(low <= high) {
        mid = (low+high)/2;

        long long part_sum = 0;
        int parts = 1;
        for (int i = 0; i < n; ++i) {
            if (part_sum + a[i] > mid) {
                part_sum = 0;
                parts++;
            } else {
                part_sum += a[i];
            }
        }

        // if no. of parts in less than (or equal to) K then mid needs to (,or can) be more constrained by reducing upper limit
        if (parts <= K) {
            high = mid - 1;
        } else { 
            low = mid + 1;
        }
    }

    return mid;
}

complexity: O (n log (sum (array))).

But since logrifs are exponentially better than linear ones, this complexity is pretty good.

worst case complexity: O (n log (INT_MAX * n)) = O (32 n + n log (n)) = O (n log (n)).

+3
source

. , N = 5 k= 3 ( , ). {1,2,8,4,9}. , k 1, ( ), .. 24, k= 5, max ( ), 9. , k . . ????? - , , like- "FFFFTTTTTTTT", T. , . ( , ). K, . low = max ( ), low = 9 high = sum ( ), .. high = 24. mid = 16, min_max_dist = 16 k. k> K, , min_max_dist . , + 1, k <= K, , min_max_dist, , , . , : -

    low           high               mid       number_of_k
    9             24                 16         9
    9             16                 12         2
    9             12                 10         4
    11            12                 11         3

, , result-> = 11 ..

    #include <bits/stdc++.h>
    #define ll long long int
    using namespace std;

    ll number_of_k(ll arr[],ll n,ll minimum_max__dist){
       ll sum=0;
       ll k=1;
       for(ll i=0;i<n;i++){
          sum+=arr[i];
         if(sum > minimum_max__dist){
           sum=arr[i];
            k++;
          }
      }
    return k;
   }

    ll Max(ll arr[], ll n)
    {
       ll max1 = -1;
       for (ll i = 0; i < n; i++)
          if (arr[i] > max1)
              max1 = arr[i];
      return max1;
    }

    ll Sum(ll arr[], ll n)
    {
      ll sum = 0;
       for (int i = 0; i < n; i++)
       sum += arr[i];
       return sum;
     }

       ll min_max_bin_search(ll arr[],ll n,ll k){
         ll low=Max(arr,n);
         ll high=Sum(arr,n);
         ll mid;
while(low<high){
     mid=low+(high-low)/2;
    if(number_of_k(arr,n,mid)<=k)
       high=mid;
    else
        low=mid+1;
     }
  return low;
  }


 int main()
 {
      ll n,k;
       cin>>n>>k;
       ll arr[n];
       for(ll i=0;i<n;i++)cin>>arr[i];
       cout<<min_max_bin_search(arr,n,k)<<endl;

   return 0;
 }

is-> O (nlogn)

Search-> https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/ problem-> http://www.spoj. //AGGRCOW/

+1

:

DP[n,m] C[1..n] m. .

DP[n,1] = sum(C1:Cn)
DP[n,n] = max(C1:Cn)
DP[n,m] = min( sum(Ck:Cn) + DP[k-1,m-1] )
          where k goes from m to n

:
DP[n,1] - , 1 - ( 1 n).
DP[n,n]. , , - , .
DP[n,m] - . , , .

0

Separation is simply a brute force problem. You should focus on features to minimize. So you need to minimize the deviation from the average.

int sum_arr(int *arr,int size)
{
    int res=0;
    while (size-->0)
       res+=*arr++;
   return res;
}
int   minimize( const int *array, int size)
{
    int i,j,cur_i;
    double dev, cur_min,avg=(double)sum_arr(array,size)/size;

    for (i=1;i<size-1;i++)
      {
         dev=abs(avg-sum_arr(array,i));
         dev+=abs(avg-sum_arr(array+i,size-i);
         if (dev<cur_min)
         {
              cur_min=dev;
               cur_i=i;
         }
      }
     return cur_i;
}

Simple code would be: (not verified)

0
source

Source: https://habr.com/ru/post/1656470/


All Articles