Employment Planning
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3005 accepted submission (s): 1196
Problem descriptiona project manager wants to determine the number of the workers needed in every month. he does know the minimal number of the workers needed in each month. when he hires or fires a worker, there will be some extra cost. once a worker
Is hired, he will get the salary even if he is not working. the manager knows the costs of hiring a worker, firing a worker, and the salary of a worker. then the manager will confront such a problem: how many workers he will hire or fire each month in order
To keep the lowest total cost of the project.
Inputthe input may contain several data sets. each data set contains three lines. first line contains the months of the project planed to use which is no more than 12. the second line contains the cost of hiring a worker, the amount
The salary, the cost of firing a worker. the third line contains several numbers, which represent the minimal number of the workers needed each month. the input is terminated by line containing a single '0 '.
Outputthe output contains one line. The minimal total cost of the project.
Sample Input
3 4 5 610 9 110
Sample output
199
Sourceasia 1997, Shanghai (Mainland China)
Recommendignatius
Solution: this topic is about dynamic planning. The number of people processed each month should be the minimum number of people needed this month, and the maximum number of people needed for this period is the maximum number of months. There may be a large number of people in the last month. This month requires a small number of people and a large number of people in the next month. In this case, we need to consider the recruitment and termination costs. Therefore, when dealing with the number of people, each month is not processed, but a batch. The process should be: J individuals (j = EMP [I]; j <= max1; j ++) hired in the I-th month );
State transition equation: Pay [I] [J] = pay [I] [J] <temp? Pay [I] [J]: temp; where temp = pay [I-1] [k] + J * Work + (cost required for recruitment or dismissal)
# Include <stdio. h ># include <algorithm> using namespace STD; int main () {int EMP [13]; // store the number of people needed each month int pay [13] [1010]; // pay [I] [J] minimum charge for the storage of J individuals hired in the I month int max1; // maximum number of people to be hired in all periods int hir, work, and FIR; // The money it takes to hire a person, hire a person, and fire a person. Int temp; int N, I, J, K; while (scanf ("% d", & N) & N) {scanf ("% d", & hir, & work, & fir); max1 = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & EMP [I]); max1 = max1> EMP [I]? Max1: EMP [I]; // find the maximum number of students} for (I = 0; I <= N; I ++) // The initial value of the boundary data is pay [I] [0] = 0; for (I = 0; I <= max1; I ++) // For the first hires, the recruitment fee is paid [0] [I] = I * hir; EMP [0] = 0; for (I = 1; I <= N; I ++) {for (j = EMP [I]; j <= max1; j ++) // minimum cost required to hire J workers in the I month {Pay [I] [J] = 0 xfffffff; // The value to be processed is initialized to the maximum value for (k = EMP [I-1]; k <= max1; k ++) // for the minimum cost required to hire J workers in the first month, retrieve information on the total cost of all persons that may be hired in the last month, take the minimum value {temp = pay [I-1] [k] + J * work; // all previous spending plus the cost of hiring this month if (k> J) // check whether the number of people recruited or dismissed last month is relative to the number of people in the previous month. The calculation fee is te MP + = (k-j) * FIR; else if (k <j) temp + = (J-k) * hir; pay [I] [J] = pay [I] [J] <temp? Pay [I] [J]: temp; // retrieve the information about the total number of people that may be hired in the previous month, take the minimum value }}} sort (pay [N] + EMP [N], pay [N] + max1 + 1); // sort the spending of the last month to find the minimum value printf ("% d \ n ", pay [N] [EMP [N]);} return 0 ;}