Fair Cut


Problem Statement :


Li and Lu have n integers, a1,a2, ...,an, that they want to divide fairly between the two of them. They decide that if Li gets integers with indices I = {i1, i2, ...,  ik} (which implies that Lu gets integers with indices J = {1, 2, ...,n}\I), then the measure of unfairness of this division is:
    f(I) =  Σ(i∈I) Σ(j∈J)|ai-aj|
Find the minimum measure of unfairness that can be obtained with some division of the set of integers where Li gets exactly k integers.

Note A\B means Set complement

Input Format

The first line contains two space-separated integers denoting the respective values of  n(the number of integers Li and Lu have) and k (the number of integers Li wants).
The second line contains n space-separated integers describing the respective values of a1, a2, ..., an.

Constraints

1 <= k < n < =3000
1 <= ai <= 10^9
For 15% of the test cases, n <=20.
For 45% of the test cases, n <=40.
Output Format

Print a single integer denoting the minimum measure of unfairness of some division where Li gets k integers.



Solution :



title-img


                            Solution in C :

In C++ :





#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

const ll Inf = 4000000000000000000;
const int Maxn = 3005;

int n, k;
int a[Maxn];
ll dp[Maxn][Maxn];

int main() {
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    fill((ll*)dp, (ll*)dp + Maxn * Maxn, Inf);
    dp[0][0] = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= i && j <= k; j++) if (dp[i][j] < Inf) {
            int my = j, his = i - j;
            if (my < k) {
                ll add = ll(a[i]) * (his - (n - k - his));
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + add);
            }
            if (his < n - k) {
                ll add = ll(a[i]) * (my - (k - my));
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + add);
            }
        }
    printf("%lld\n", dp[n][k]);
    return 0;
}








In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
  private static long[] DIFF;
  private static long[] DATA;
  private static int COUNT;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
         COUNT = sc.nextInt();
        DATA = new long[n];
        for(int i = 0 ; i < n ; i++) {
            DATA[i] = sc.nextLong();
        }
        
        
      COUNT = Math.min(DATA.length - COUNT, COUNT);
     DIFF = new long[DATA.length];
     System.out.println(calc2());
    }
     
    
     private static long calc2() {
    Arrays.sort(DATA);

    long[] sum = new long[DATA.length + 1];
    for (int i = 1; i <= DATA.length; i++) {
      sum[i] = sum[i - 1] + DATA[i - 1];
    }

    for (int i = 0; i < DATA.length; i++) {
      for (long aT : DATA) {
        DIFF[i] += Math.abs(DATA[i] - aT);
      }
    }

    return Math.min(clc(0), clc(1));
  }

  private static long clc(int i) {
    long chSum = 0;
    long summ0 = 0;

    for (int j = 0; j < DATA.length; j++) {
      if (!valid(j, i)) {
        continue;
      }
      chSum += DATA[j];

      for (int m = 0; m < DATA.length; m++) {
        if (!valid(m, i)) {
          summ0 += Math.abs(DATA[j] - DATA[m]);
        }
      }
    }

    long min = summ0;
    int first = i;
    int next = 2 * COUNT + i;
    while (next < DATA.length) {

      chSum -= DATA[first];


      summ0 -= DIFF[first];
      summ0 += 2 * (chSum - DATA[first] * (COUNT - 1));


      summ0 += DIFF[next];
      summ0 -=  2 * (DATA[next] * (COUNT - 1) - chSum);


      chSum += DATA[next];



      min = Math.min(summ0, min);
      next += 2;
      first += 2;
    }

    return min;
  }

  private static boolean valid(int j, int i) {
    return j % 2 == i && j < COUNT * 2;
  }
}








In C :





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int n;
int k;
int a[3001];
long long listcost[3001];
long long allcost[3001];

int main()
{
    int i;
    int j;
    int besti;
    long long bestscore;
    long long allscore;
    long long tmp;

    scanf("%d %d", &n, &k);
    if (k > n - k) {
        k = n - k;
    }
    for (i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    for (i = 1; i < n; ++i) {
        tmp = a[i];
        j = i;
        while (j) {
            if (tmp > a[j-1]) {
                break;
            }
            a[j] = a[j-1];
            j -= 1;
        }
        a[j] = tmp;
    }
    for (i = 0; i < n; ++i) {
        listcost[i] = 0;
        allcost[i] = 0;
    }
    for (i = 0; i < n; ++i) {
        for (j = 0; j < n; ++j) {
            if (a[i] > a[j]) {
                allcost[i] += a[i] - a[j];
            } else {
                allcost[i] += a[j] - a[i];
            }
        }
    }
    allscore = 0;
    bestscore = 0;
    for (i = 0; i < k; ++i) {
        allscore = bestscore;
        besti = 0;
        bestscore = 1000LL*1000LL*1000LL*1000LL*1000LL;
        for (j = 0; j < n; ++j) {
            tmp = allcost[j];
            tmp -= listcost[j];
            tmp -= listcost[j];
            tmp += allscore;
            if (tmp < bestscore) {
                bestscore = tmp;
                besti     = j;
            }
            if (tmp == bestscore) {
                if (listcost[j] > listcost[besti]) {
                    besti = j;
                }
            }
        }
        tmp = allcost[besti];
        allcost[besti] = allcost[n - 1];
        allcost[n - 1] = tmp;
        tmp = listcost[besti];
        listcost[besti] = listcost[n - 1];
        listcost[n - 1] = tmp;
        tmp = a[besti];
        a[besti] = a[n - 1];
        a[n - 1] = (int) tmp;
        n -= 1;
        j = besti;
        while (j < n - 1) {
            if (a[j] < a[j+1]) {
                break;
            }
            tmp = allcost[j];
            allcost[j] = allcost[j+1];
            allcost[j+1] = tmp;
            tmp = listcost[j];
            listcost[j] = listcost[j+1];
            listcost[j+1] = tmp;
            tmp = a[j];
            a[j] = a[j+1];
            a[j+1] = (int) tmp;
            j += 1;
        }
        // update listcost
        for (j = 0; j < n; ++j) {
            if (a[j] > a[n]) {
                listcost[j] += a[j] - a[n];
            } else {
                listcost[j] += a[n] - a[j];
            }
        }
    }
    printf("%lld\n", bestscore);

    return 0;
}








In python3 :





import sys

def solve(a, k):
    def sumup(I, J):
        sm = 0
        for u in I:
            for v in J:
                sm += abs(u - v)
        return sm
    k = min(k, len(a) - k)
    a.sort()
    print('k=%d a=%s' % (k, a), file = sys.stderr)
    index = (len(a) - 2 * k) // 2
    I = a[index:index + 2 * k:2]
    J = a[:index] + a[index + 1:index + 2 * k + 1:2] + a[index + 2 * k:]
    print('I=%s J=%s' % (I, J), file = sys.stderr)
    return sumup(I, J)

_, k = map(int, input().split())
a = list(map(int, input().split()))
print(solve(a, k))
                        








View More Similar Problems

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →

Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →

Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →