Employment Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3034 Accepted Submission(s): 1210
Problem Description
A 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.
Input
The 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 of 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'.
Output
The output contains one line. The minimal total cost of the project.
Sample Input
Sample Output
Source
Asia 1997, Shanghai (Mainland China)
Recommend
Ignatius
一道比较暴力的DP,居然没有给范围
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <iomanip>
using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1|1
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
#define mid ((l + r) >> 1)
#define mk make_pair
const int MAXN = 15;
const int maxw = 100 + 20;
const int MAXNNODE = 10000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 10007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
const D e = 2.718281828459;
int dp[MAXN][maxw] , num , aim[MAXN] , hire , fire , salary , maxnum;///dp[i][j]表示第i个月j个员工的最小花费
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
int n;
while(~scanf("%d" , &n) , n)
{
scanf("%d%d%d" , &hire , &salary , &fire);
maxnum = 0;
FOR(i , 0 , n)
{
scanf("%d" ,&aim[i]);
maxnum = max(maxnum , aim[i]);
}
FORR(i , aim[0] , maxnum)dp[0][i] = i * (hire + salary);
num = 0;
FOR(i , 1 , n)
{
FORR(j , aim[i] , maxnum)///枚举该月雇多少人
{
dp[i][j] = INF;
FORR(k , aim[i - 1] , maxnum)
{
if(k > j)dp[i][j] = min(dp[i][j] , dp[i - 1][k] + (k - j) * fire + j * salary);///解雇k - j个人
else if(k == j)dp[i][j] = min(dp[i][j] , dp[i - 1][j] + j * salary);
else dp[i][j] = min(dp[i][j] , dp[i - 1][k] + (j - k) * hire + j * salary);///雇佣j - k个人
}
}
}
int ans = INF;
FORR(i , aim[n - 1] , maxnum)ans = min(ans , dp[n - 1][i]);///最后一个月的最小花费
printf("%d\n" , ans);
}
return 0;
}