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

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →