Employment Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4846    Accepted Submission(s): 2061

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
3
4 5 6
10 9 11
0
 
Sample Output
199
 
之前做过一次,这次算复习,还是花了不少时间。
dp[i][j]表示第i个月留j个人的最优解。每个月保留mon[i]—maxn的解用于下一次更新。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 999999999
int mon[];
int dp[][];
int main()
{
int n,hire,fire,sala;
while(scanf("%d",&n)!=EOF&&n)
{
//memset(dp,0,sizeof(dp));
scanf("%d%d%d",&hire,&sala,&fire);
int maxn=;
for(int i=; i<=n; i++)
{
scanf("%d",&mon[i]);
if(maxn<mon[i])
maxn=mon[i];
}
for(int i=; i<=; i++)
for(int j=; j<=; j++)
dp[i][j]=INF;
for(int i=; i<=n; i++)
for(int j=mon[i]; j<=maxn; j++)
if(i==)
dp[i][j]=j*(sala+hire);
else
{
for(int k=mon[i-]; k<=maxn; k++)
if(j>=k)
dp[i][j]=min(dp[i][j],dp[i-][k]+(j-k)*(sala+hire)+k*sala);
else
dp[i][j]=min(dp[i][j],dp[i-][k]+(k-j)*fire+j*sala);
}
//cout<<dp[1][10]<<'*'<<dp[1][11]<<endl;
// cout<<dp[2][9]<<'*'<<dp[2][10]<<'*'<<dp[2][11]<<endl;
// cout<<dp[3][11]<<endl;
int ans=INF;
for(int i=mon[n]; i<=maxn; i++)
if(ans>dp[n][i])
ans=dp[n][i];
printf("%d\n",ans);
} return ;
}

HDU_1158_Employment Planning_dp的更多相关文章

随机推荐

  1. 从尾到头打印链表——剑指Offer

    https://www.nowcoder.net/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage= ...

  2. putty SSH出现乱码

    解决方法如下: 打开PuTTY主程序 选择window-〉Appearance-〉Font settings-〉点击Change.按钮,字体中选择"新宋体". 选择window-〉 ...

  3. 重啓ubuntu后 VNC 自動運行

    Vino-Server是Ubuntu自带的有個缺点:重启后不能自動運行(可能是基於安全吧!) 親身測試对象:windows & ubuntu 10.04已安装图形桌面gnome ***wind ...

  4. Swift之闭包

    swift中闭包是一个非常强大的东西,闭包是自包括的函数代码块,能够在代码中被传递和使用.跟C 和 Objective-C 中的代码块(blocks)非常相似 .这个大家必须掌握!必须掌握! 必须掌握 ...

  5. 怎样一步步用D3画多曲线

    Bar Chart: http://bl.ocks.org/mbostock/3885304 这是一个画柱状图的基本形式. Axis是数轴: tickets是数轴上的标尺.tickets第二个參数% ...

  6. iOS 打开扬声器以及插入耳机的操作

    废话不多说说一下现状 网上好多关于扬声器的操作,可是问题多多.SDK7.X 和SDK7.X以上版本号有点诧异 #import <Foundation/Foundation.h> #impo ...

  7. JAVA学习(三):Java基础语法(变量、常量、数据类型、运算符与数据类型转换)

    Java基础语法(变量.常量.数据类型.运算符与数据类型转换) 1.变量 Java中.用户能够通过指定数据类型和标识符来声明变量.其基本的语法为: DataType identifier; 或 Dat ...

  8. Java程序执行超时——Future接口介绍

    在Java中,如果需要设定代码执行的最长时间,即超时,可以用Java线程池ExecutorService类配合Future接口来实现. Future接口是Java标准API的一部分,在java.uti ...

  9. HDU 5311 Sequence

    Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  10. POJ 2007 Scrambled Polygon(简单极角排序)

    水题,根本不用凸包,就是一简单的极角排序. 叉乘<0,逆时针. #include <iostream> #include <cstdio> #include <cs ...