The Blacklist
Problem Statement :
A new gangster is trying to take control of the city. He makes a list of his N adversaries (e.g. gangster 1, gangster 2, ... gangster N-1, gangster N) and plans to get rid of them. K mercenaries are willing to do the job. The gangster can use any number of these mercenaries. But he has to honor one condition set by them: they have to be assigned in such a way that they eliminate a consecutive group of gangsters in the list, e.g. gangster i, gangster i+1, ..., gangster j-1, gangster j, where the following is true: 1 <= i <= j <= N. While our new gangster wants to kill all of them, he also wants to pay the least amount of money. All mercenaries charge a different amount to kill different people. So he asks you to help him minimize his expenses. Input Format The first line contains two space-separated integers, N and K. Then K lines follow, each containing N integers as follows: The jth number on the ith line is the amount charged by the ith mercenary for killing the jth gangster on the list. Constraints 1 <= N <= 20 1 <= K <= 10 0 <= amount charged <= 10000 Output Format Just one line, the minimum cost for killing the N gangsters on the list.
Solution :
Solution in C :
In C++ :
#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
typedef long long int LL;
const int INF = 100000000;
int C[10][20];
int memo[1<<10][11][21];
int n,k;
int solve(int mask, int last, int idx) {
// printf("%d %d %d\n",mask,last,idx);
if(last == -1) {
int res = INF;
rep(i,0,k) res = min(res, C[i][idx] + solve(mask|(1<<i), i, idx+1));
return res;
}
if(idx == n) return 0;
int &res = memo[mask][last][idx];
if(memo[mask][last][idx] != INF)
return memo[mask][last][idx];
res = C[last][idx] + min(res, solve(mask, last, idx+1));
rep(i,0,k) if(!((mask>>i)&1)) {
res = min(res, C[i][idx] + solve(mask|(1<<i) , i, idx+1) );
}
return res;
}
int main() {
scanf("%d%d",&n,&k);
rep(i,0,1<<k) rep(j,0,k) rep(l,0,n) memo[i][j][l] = INF;
rep(i,0,k) rep(j,0,n) S(C[i][j]);
printf("%d\n",solve(0, -1, 0));
return 0;
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int K = input.nextInt();
int[][] price = new int[K][N];
for (int k=0; k<K; k++) {
for (int n=0; n<N; n++) {
price[k][n] = input.nextInt();
}
}
int limit = 1<<K;
int[][] dp = new int[N+1][limit+1];
for (int n=0; n<=N; n++) {
for (int x=0; x<=limit; x++) {
dp[n][x] = 1000000;
}
}
dp[0][0] = 0;
for (int n=1; n<=N; n++) {
for (int k=0; k<K; k++) {
int mask = 1 << k;
for (int from=1; from<=n; from++) {
for (int x=0; x<limit; x++) {
if ((x&mask) == 0) {
int newMask = x|mask;
int newValue = dp[from-1][x];
for (int i=from; i<=n; i++) {
newValue += price[k][i-1];
}
if (dp[n][newMask] > newValue) {
dp[n][newMask] = newValue;
}
}
}
}
}
}
int min = Integer.MAX_VALUE;
for (int value : dp[N]) {
min = Math.min(min, value);
}
System.out.println(min);
}
}
In C :
#include<stdio.h>
#include<limits.h>
#define MIN(x,y) x<y?x:y
int res,n,k,a[15][25]={0};
void find(int curr,int d[],int i,int sumsofar)
{
//printf("here %d %d %d %d\n",curr,i,sumsofar,res);
if(sumsofar>=res)
return;
if(curr>n)
{
res=sumsofar;
return;
}
int p[25],x=0;
//find(curr+1,d,i,sumsofar+a[i][curr]);
int j,l;
for(j=1;j<=k;j++)
if(d[j]==0)
p[x++]=j;
for(l=curr;l<=n;l++)
{
for(j=0;j<x;j++)
if(sumsofar+a[p[j]][l]<sumsofar+a[i][l] && sumsofar+a[p[j]][l]<res)
{
// printf("here also %d %d\n",p[j],i);
d[p[j]]=1;
find(l+1,d,p[j],sumsofar+a[p[j]][l]);
d[p[j]]=0;
}
sumsofar+=a[i][l];
if(sumsofar>=res)
return;
}
res=sumsofar;
}
int main()
{
int i,j;
scanf("%d%d",&n,&k);
for(i=1;i<=k;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
/*for(i=1;i<=k;i++)
{for(j=1;j<=n;j++)
{
b[i][j]=b[i][j-1]+a[i][j];
//if(i!=1)
for(j1=1;j1<=i;j1++)
b[i][j]=MIN(b[i][j],(b[j1][j-1]+a[i][j]));
// printf("%d ",b[i][j]);
}
// printf("\n");
}*/
//int min=INT_MAX;
/*for(i=1;i<=k;i++)
if(b[i][n]<min)
min=b[i][n];*/
res=INT_MAX;
int d[25]={0};
for(i=1;i<=k;i++)
{
d[i]=1;
find(1,d,i,0);
d[i]=0;
}
printf("%d\n",res);
return 0;
}
In Python3 :
G, M = [int(s) for s in input().split()]
costs = list()
cache = dict()
for i in range(M):
costs.append([int(s) for s in input().split()])
def kill(gn, imercs):
key = str(gn)+str(imercs)
if key in cache:
return cache[key]
mercs = imercs[:]
if gn == 0:
return 0
temp = list()
for m in mercs:
tempm = mercs[:]
tempm.remove(m)
if len(tempm) == 0:
temp.append(sum(costs[m][:gn]))
else:
for c in range(gn - 1, -1, -1):
temp.append(sum(costs[m][c:gn]) + kill(c, tempm))
cache[key] = min(temp)
return min(temp)
print(str(kill(G, [i for i in range(M)])))
View More Similar Problems
Maximum Element
You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each
View Solution →Balanced Brackets
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra
View Solution →Equal Stacks
ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of
View Solution →Game of Two Stacks
Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f
View Solution →Largest Rectangle
Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle
View Solution →Simple Text Editor
In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,
View Solution →