题目链接:

acm.hdu.edu.cn/showproblem.php?pid=1158

Employment Planning

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

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
 
Source
 
 
题意:
一家公式完成一个项目,需要n个月的时间,每个月至少雇佣a1...an个工人,解雇工人和雇佣工人都需要一定的费用,每个月还要给雇来的工人工资,问你最少的花费是多少(不用输出方案,只有输出最优解)
分析:
解雇和雇佣工人都需要一定的费用,现在就是要看每个月怎么对工人进行解雇和雇佣的操作可以使得花费最少,每个月雇佣的工人可以>=这个月必须雇佣的工人数目,具体拿样例分析
3
4 5 6
10 9 11
代表3个月份,雇佣工人耗费4,一个雇佣的工人每月工资5,解雇工人耗费6
第一个月至少雇佣10个人,第二个月至少雇佣9个人,第三个月至少雇佣11个人
做法:
该3个月份中最大工人数目为11,最小工人数目为9
第一个月我们可以雇佣10个工人,或者11个工人
第二个月我们需要的工人底线是9个,如果该月雇佣9个(需要解雇x人,x由上一个月雇佣人数决定(上一个月人数减去9),如果该月雇佣10个人(需要解雇人数x,也由上一个月雇佣人数决定),如果该月雇佣11个人,那么现在我们可能还需要继续的雇佣工人,
第三个月也依次类推,
这样不同的雇佣策略导致我们的花费肯定不同,在这些方案在花费中找到最小的即可
 
dp的关键在于明白:前面一个月雇佣多少个人使得这个月的花费最小
 dp[i][j] :代表在第i个月的时候,雇佣J个人的总费用(雇佣费+工资)
dp[i][j]=dp[i-1][k](前面一个月雇佣k个人,可以使得前面的i个月(包括i)的花费最小)+这个月的费用
          =dp[i-1][k]+abs(j-k)*v+j*工资  v表示解雇或者雇佣的权值,由j,k相对大小决定
注意点:
1.要知道每月需要工人的max和min(保证每月雇佣人数在这个集合之间,[min,max]),这样每个月雇佣的人数不同,花费也不同,才可以求解最优解
2.初始化 第一个月的时候需要手动初始化,拿样例说,dp[1][9]第一个月雇佣9个人的费用(雇佣费加工资).......................dp[1][11]第一个月雇佣11个人的费用(雇佣费加工资),需要手动初始化
3.最有解是在dp[n][j]中找最小的,j属于[min,max]集合
 
重点:前面一个月雇佣多少人使得前面月份加上这个月份的花费最小
 
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define inf 9999999
int dp[][];
int a[];
int hire,salary,fire;
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==)
break;
scanf("%d %d %d",&hire,&salary,&fire);
int max_people=-inf,min_people=inf;
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
max_people=max(max_people,a[i]);
min_people=min(min_people,a[i]);
}
//dp[i][j] 在第i个月的时候,雇佣j个人的总费用(雇佣费+工资) //dp初始化
memset(dp,0x3f,sizeof(dp));//无穷大
for(int j=min_people; j<=max_people; j++)
{
dp[][j]=hire*j+salary*j;
} for(int i=; i<=n; i++)
{
for(int j=a[i]; j<=max_people; j++)
{
int t=inf;
for(int k=a[i-]; k<=max_people; k++)
{
int v=hire;
if(k-j>)//解雇
{
v=fire;
}
int x=dp[i-][k]+abs(k-j)*v+j*salary;
t=min(t,x);
}
dp[i][j]=t;
}
}
int t=inf;
for(int j=; j<=max_people; j++)
{
t=min(t,dp[n][j]);
}
printf("%d\n",t);
}
}

ojbk,溜了溜了,吃饭去

 
 
 
 
 
 
 
 
 
 

HDU 1158(非常好的锻炼DP思维的题目,非常经典)的更多相关文章

  1. HDU 1158 Employment Planning (DP)

    题目链接 题意 : n个月,每个月都至少需要mon[i]个人来工作,然后每次雇佣工人需要给一部分钱,每个人每个月还要给工资,如果解雇人还需要给一笔钱,所以问你主管应该怎么雇佣或解雇工人才能使总花销最小 ...

  2. hdu 1158 dp Employment Planning

    Employment Planning Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  4. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

  5. hdu 4778 Gems Fight! 博弈+状态dp+搜索

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...

  6. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  7. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  8. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. Redis概述与安装

    一.什么是Redis ​ 由c语言编写的,以键值对的形式存储的数据库. 缓存技术(驻留在内存中) ​ key:value ​ 支持5种数据类型: ​ String ​ Hash(哈希表) ​ list ...

  2. 粗粒度权限控制(拦截是否登录、拦截用户名admin权限)

    RBAC --> 基于角色的权限控制 tb_user tb_role tb_userrole tb_menu(增.删.改.查) tb_rolemenu 1 说明     给出三个页面:index ...

  3. sublime_text3 快速生成xhtml表头手动设置

    1. 在github上手动下载emmet安装插件 https://github.com/sergeche/emmet-sublime#how-to-install 2.把下载的安装插件放到packag ...

  4. 关于html 中form表单的内标签和使用

    表单标记 1.普通文本框: <input type=”text” name=”名称” value=”值”;不写value默认为空/> 2.密码框:<input type=”passw ...

  5. MyEclipse打包带源码的jar包

    平时开发中,我们喜欢将一些类打包成jar包,然后在别的项目中继续使用,不过由于看不到jar包里面的类的源码了,所以也就无法调试,要想调试,那么就只能通过关联源代码的形式,这样或多或少也有一些不方便,今 ...

  6. JS全国城市三级联动

    HTML <select id="s_province" name="s_province"></select> <select ...

  7. Esri大数据分析引擎GeoAnalytics Server部署经历

    系统架构 Base WebGIS 4Cores 16GB Spatiotemporal Data Store 32GB SSD Disk 足够大的空间 GA Server 4Cores 16GB 足够 ...

  8. 【Angularjs】ng-repeat中使用ng-model遇到的问题

    总结:在ng-repeat中ng-model的问题,原因是ng-model对controller中的$scope是不可见的,所以在使用repeat中的某个对象的属性的时候,最好还是将该对象或者该对象的 ...

  9. 使用ZXing实现扫描多个条形码页面

    1.前言 ZXing是google官方推出的跨平台的基于Java实现处理扫面二维码或者条形码的库.支持很多格式,一维条码支持UPC-A,UPC-E,EAN-8,Code 39,Code 93等格式,二 ...

  10. volley6--CacheDispatcher从缓存中获取数据

    源码: /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, V ...