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 ;
}

最新文章

  1. DrawSVG - SVG 路径动画 jQuery 插件
  2. BOM之navigator、history、screen对象
  3. 二十二、android中application标签说明
  4. Pomelo实现最简单的通信-egret。
  5. greenplum和postgresql
  6. [转] Java中的访问控制
  7. HTML5+AJAX+WEBAPI 文件上传
  8. MMORPG战斗系统随笔(三)、AI系统简介
  9. Python内置函数(55)——globals
  10. 【Python】*args和**kwargs的区别
  11. rabbitmq在ios中实战采坑
  12. springboot 常见请求方式
  13. TP5上传图片
  14. CF1131E String Multiplication(???)
  15. 多线程下载英文Google地图
  16. Linux 文件系统的目录定义
  17. RocketMQ读书笔记6——可靠性优先的使用场景
  18. OCP试题解析之053-17 CONFIGURE CONTROLFILE AUTOBACKUP ON
  19. pthread_join和pthread_detach的用法
  20. java 高并发解决方案

热门文章

  1. Microsoft SQL Server Query Processor Internals and Architecture
  2. 深入解析Microsoft Sql server 2008
  3. 【CV论文阅读】Going deeper with convolutions(GoogLeNet)
  4. 1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.
  5. 闲聊ROOT权限——ROOT权限的前世今生
  6. ExtJs--09--javascript对象的方法的3种写法 prototype通过原型设置方法效率最好
  7. Codeforces #2B The least round way(DP)
  8. MongoDB集群——分片
  9. ROS人脸检测 使用webcam实现
  10. 解决:sql2005 安装完后 没有服务的问题