To collect the mineral ore, the company regroups the N produced heaps into a smaller number of K heaps, each located at one of the initial mining points. The newly formed heaps are then collected by trucks.
To regroup the N heaps, they use a barge, which in practice can carry any amount of mineral ore because it is very large. The barge starts at the river source and can only travel downriver, so the heap produced at a mining point X can be taken to a mining point Y only if Y > X. Each heap is moved completely to another mining point, or not moved at all. The cost of moving a heap of weight W from a mining point X to a mining point Y is W x (Y - X). The total cost of the regrouping is the sum of the costs for each heap movement. Notice that a heap which is not moved has no influence on the total cost.
Given the values for N and K, the N mining points, and the weight of the heap each mining point produced, write a program that calculates the minimum total cost to regroup the N initial heaps into K heaps.
Input
Each test case is described using several lines. The first line contains two integers N and K denoting respectively the number of initial heaps and the desired number of heaps after regrouping ( 1Sample Output
For each test case output a line with an integer representing the minimum total cost to regroup the N initial heaps into K heaps.Sample Input
3 1 20 1 30 1 40 1 3 1 11 3 12 2 13 1 6 2 10 15 12 17 16 18 18 13 30 10 32 1 6 3 10 15 12 17 16 18 18 13 30 10 32 1
Sample Output
30 8 278 86
//Notese que siempre se dejara fijo el ultimo monton
ResponderEliminar#include<stdio.h>
long long a[1001],y,b[1001],n,k,min,c[1001][1001],d[1001],p[1001],x,i,j,s;
int main(){
while(scanf("%lld %lld",&n,&k)==2){
for(i=1;i<=n;i++){
scanf("%lld %lld",&d[i],&p[i]);
a[i]=a[i-1]+p[i];//suma de los pesos hasta el punto i
b[i]=b[i-1]+d[i]*p[i];//costo de mover los primeros i-1 montones al punto i
}
//c[i][j]=el menor costo posible de dejar j montones de los primeros i
for(i=2;i<=n;i++){
c[i][1]=d[i]*(a[i-1])-b[i-1];
y=k+i-n;
if(2>y)
y=2;
for(j=y;j<i&&j<=k;j++){
min=d[i]*(a[i-1]-a[j-1])-b[i-1]+b[j-1];
for(s=j;s<=i-1;s++){
x=c[s][j-1]+d[i]*(a[i-1]-a[s])-b[i-1]+b[s];
if(x<min)
min=x;
}
c[i][j]=min;
}
}
printf("%lld\n",c[n][k]);
}
}