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

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →