[经典DP]Employment Planning

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

    
    
3 4 5 6 10 9 11 0
 

Sample Output

    
    
199

大致题意:

第一行为N ,代表的季度(每个季度都是一个状态);

第二行有三个数字, hir (招聘费),sal(月薪) , fir(解雇费) ;

第三行有N个数字, 代表每个季度应该需要多少个人;


如何寻求状态方程:

对于本题,状态是针对当前(这个季度),和上一个状态(上个季度);且被上个季    度的人数影响到本季度是否招聘还是解雇;

因此有:num[13] :代表12个月(最多),每个季度需要多少个人;

      dp[i][j]: 代表第i季度需求保留j个人;

状态方程:(1)如果当前状态的需求人数大于上个季度的人,则应该雇人,所以资金            费用就是  :上个季度的人的最低消费+本季度新加人的雇佣费+本季度     需求人的工资   

                   (2) 反之,如果上个季度的人多于这个季度,则就要裁人;费用就是:        上个季度的最低消费+本季度裁的人数*遣送费+本季度人*工资

对于(1) dp[i-1][k]+(j-k)*hiring+j*salary;

          (2)   dp[i-1][k]+(k-j)*firing+j*salary;

只需要在两者之间找出最小值,就是dp[i][j]  ,最后遍历找到最小值就行;


AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int dp[20][500];//*第i阶段保留j个人;
int hiring , salary , firing; //*雇佣工资,月薪,解聘工资;
int num[13];//*阶段人数
int main()
{
    int month ;
    while(cin>>month, month)
    {
        memset(dp,0,sizeof(dp));
        memset(num , 0  ,sizeof(num));
        int maxi = - 1 ;
        cin>>hiring>>salary>>firing;
        for(int i = 1 ; i <=month ;i++)
        {
            cin>>num[i];//*每个阶段的人数;
            if(maxi<num[i])
            {
                maxi = num[i]; //*寻求最大雇佣人数 ;
            }
        }

        for(int i= num[1] ; i<=maxi;i++) //*更新初始状态;
        {
            dp[1][i] = i*hiring + i*salary; //*第一个月基础工资
        }
        int mini , cost ;
        for(int i= 2 ; i<= month ; i++)
        {
            for(int j = num[i] ; j<=maxi ; j++)
            {
                mini = 9999999 ;
                for(int k = num[i-1] ; k <=maxi ; k++)
                {
                    if(k<=j)
                        cost = dp[i-1][k]+(j-k)*hiring+j*salary;
                    else cost =  dp[i-1][k]+(k-j)*firing+j*salary;
                    if(mini >cost )
                        mini = cost ;
                }
                dp[i][j] = mini ;
            }
        }
        mini = 99999999 ;
        for(int i = num[month]; i<=maxi;i++)
        {
            if(dp[month][i] < mini )
            {
                mini = dp[month][i] ;
            }
        }

        cout << mini <<endl;
    }
    return 0 ;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kelisita

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值