Mining
Problem Statement :
There are n gold mines along a river, and each mine i produces wi tons of gold. In order to collect the mined gold, we want to redistribute and consolidate it amongst exactly k mines where it can be picked up by trucks. We do this according to the following rules: You can move gold between any pair of mines (i.e., i and j, where 1 <= i <j <= n). All the gold at some pickup mine i must either stay at mine i or be completely moved to some other mine, j. Move w tons of gold between the mine at location xi and the mine at location xj at a cost of |xi-xj|*w. Given n, k, and the amount of gold produced at each mine, find and print the minimum cost of consolidating the gold into k pickup locations according to the above conditions. Input Format The first line contains two space-separated integers describing the respective values of n (the number of mines) and k (the number of pickup locations). Each line i of the n subsequent lines contains two space-separated integers describing the respective values of xi (the mine's distance from the mouth of the river) and wi (the amount of gold produced in tons) for mine i. Note: It is guaranteed that the mines are will be given in order of ascending location. Constraints 1 <= k < n <= 5000 1 <= wi,xi <= 10^6 Output Format Print a single line with the minimum cost of consolidating the mined gold amongst k different pickup sites according to the rules stated above.
Solution :
Solution in C :
In C++ :
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0);
for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head
const int M=5010;
ll wt[M],dp[M][M],w[M][M],sw[M],scw[M];
int s[M][M],cord[M];
int n,p;
ll calc(int i, int j,int k) {
assert(i<=k&&k<=j);
return cord[k]*(sw[k]-sw[i-1])-(scw[k]-scw[i-1])+
scw[j]-scw[k-1]-cord[k]*(sw[j]-sw[k-1]);
}
int main() {
scanf("%d%d",&n,&p);
rep(i,1,n+1) {
scanf("%d%lld",cord+i,wt+i);
}
rep(i,1,n+1) sw[i]=sw[i-1]+wt[i],scw[i]=scw[i-1]+cord[i]*wt[i];
rep(i,1,n+1) {
int p=i;
rep(j,i+1,n+1) {
while (p<j&&calc(i,j,p)>calc(i,j,p+1)) ++p;
w[i][j]=calc(i,j,p);
}
}
rep(i,1,n+1) dp[1][i]=w[1][i],s[i][i]=i-1;
rep(i,2,p+1) {
int j=n;
dp[i][n]=1ll<<60;
for (int k = s[i - 1][n]; k <= n - 1; k++) {
ll temp = dp[i - 1][k] + w[k + 1][n];
if (temp < dp[i][n]) dp[i][n] = temp,s[i][n] = k;
}
for (int j = n - 1; j >= i + 1; j--) {
dp[i][j] = 1ll<<60;
for (int k = s[i - 1][j]; k <= s[i][j + 1]; k++) {
ll temp = dp[i - 1][k] + w[k + 1][j];
if (temp < dp[i][j]) dp[i][j] = temp,s[i][j] = k;
}
}
}
printf("%lld\n",dp[p][n]);
}
In Java :
import java.io.File;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// Scanner scn = null;
// try {
// scn = new Scanner(new File("input.txt"));
// } catch (Exception ex) {
// ex.printStackTrace();
// }
int n = scn.nextInt();
int m = scn.nextInt();
int[] x = new int[n];
long[] w = new long[n];
for (int i = 0; i < n; ++i) {
x[i] = scn.nextInt();
w[i] = scn.nextLong();
}
long f[][] = new long[n][m + 1];
int b[][] = new int[n][m + 1];
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= m; ++j) {
f[i][j] = Long.MAX_VALUE / 2;
}
}
f[0][1] = 0;
long s = w[0];
for (int i = 1; i < n; ++i) {
f[i][1] = f[i - 1][1] + s * (x[i] - x[i - 1]);
s += w[i];
}
for (int j = 2; j <= m; ++j) {
for (int i = j - 1; i < n; ++i) {
int l = i;
s = w[i];
long t = w[i];
long cost = 0;
for (int k = i - 1; k >= j - 2; --k) {
cost += (s - t) * (x[k + 1] - x[k]);
s += w[k];
while (l - 1 >= k && x[i] - x[l - 1] <= x[l - 1] - x[k]) {
l--;
t += w[l];
cost += w[l] * (x[i] - x[l]) - w[l] * (x[l] - x[k]);
}
if (f[i][j] > f[k][j - 1] + cost) {
f[i][j] = f[k][j - 1] + cost;
b[i][j] = k;
}
if (k < b[i - 1][j]) {
break;
}
// System.out.println("current cost = " + (f[k][j - 1] + cost));
}
// System.out.println("+++");
}
}
long ret = f[n - 1][m];
// System.out.println("ret = " + ret);
s = w[n - 1];
long cost = 0;
for (int i = n - 2; i >= m - 1; --i) {
// System.out.println("i = " + i);
// System.out.println("fim = " + f[i][m]);
cost += s * (x[i + 1] - x[i]);
// System.out.println("cost = " + cost);
ret = Math.min(ret, f[i][m] + cost);
s += w[i];
// System.out.println("ret = " + ret);
// System.out.println("+++");
}
System.out.println(ret);
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int x[5000];
int w[5000];
int64_t val[5000][5000];
int64_t a[5000], b[5000];
int64_t mining()
{
int n, k, i;
scanf("%d %d", &n, &k);
for(i = 0; i < n; ++ i)
scanf("%d %d", &x[i], &w[i]);
for(i = 0; i < n; ++i)
{
int64_t left = 0, right = 0, acc = 0;
int s, j;
for(j = i + 1, s = i; j < n; ++ j)
{
acc += w[j] * (int64_t)(x[j] - x[s]);
right += w[j];
while(s < j && left + w[s] < right)
{
acc += (left + w[s] - right) * (int64_t)(x[s + 1] - x[s]);
left += w[s];
right -= w[s + 1];
++ s;
}
val[j][i] = acc;
}
}
/* memcpy(a, val[0], n * sizeof(int64_t)); */
for(i = 0; i < n; ++ i)
a[i] = val[i][0];
if(n * (int64_t) n * (int64_t) k < 1000000000)
{
for(; 1 < k; --k)
for(i = n - 1; -1 < i; --i)
{
int s;
a[i] = val[i][0];
for(s = 1; s < i + 1; ++ s)
{
const int64_t c = a[s - 1] + val[i][s];
if(c < a[i]) a[i] = c;
}
}
return a[n - 1];
}
for(; 1 < k; -- k)
{
int idx = 0;
memcpy(b, a, n * sizeof(int64_t));
for(i = 0; i < n; ++ i)
{
a[i] = (idx ? b[idx - 1] : 0) + val[i][idx];
for(; idx < i && b[idx] + val[i][idx + 1] < a[i]; ++ idx)
a[i] = b[idx] + val[i][idx + 1];
{
int s = i;
for(s = i; idx < s && i < s + 50; -- s)
{
const int64_t v = (s ? b[s - 1] : 0) + val[i][s];
if(v < a[i]) a[i] = v;
}
}
}
}
return a[n - 1];
}
int main()
{
printf("%lld", (long long) mining());
return EXIT_SUCCESS;
}
View More Similar Problems
Sparse Arrays
There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun
View Solution →Array Manipulation
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu
View Solution →Print the Elements of a Linked List
This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode
View Solution →Insert a Node at the Tail of a Linked List
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink
View Solution →Insert a Node at the head of a Linked List
Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below
View Solution →Insert a node at a specific position in a linked list
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e
View Solution →