Employment Planning

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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


4 5 6
10 9 11
0

Sample Output

199

这道题目必须要写一下,其实思路挺清晰的,

我一开始看到之后就有了规划方向,然后开始敲,我觉得是简单规划,用一个一维数组dp[]记录就行,因为规划方向只要从本月的上一个月那里得到+当月数据就行,所以我只用一层外层循环,里面当本月需求大于上月的时候,就只能增加招募,这没得说的,但是当本月需求小于上月,就需要遍历下fire的人数,可能是0也可能是上月-本月需求。这样得出来样例可过,但是会Wrong answer。。。我略不解,因为虽然没证明,感觉我这个策略没问题啊。。

后来找了另外种方法,他们在一维数组的基础上添加了一维成了dp[i][j],i还是代表是第几个月,j就代表此时雇佣人数,一开始读数据的时候挑出最大人数的月,作为上界,每次内部循环里就从 当前需求 到 最大人数 进行遍历挑选最优解。。。最后这样的方法过了。

我还是略有些不解,我原以为只要满足了当前需求人数,就不用再往上规划了,因为到了需要更多的人的时候再招募也行啊,而且花钱应该少些,规划了也是白费,但事实是确实需要往上规划。。我大概感觉,可能数据里面会有这样的变态数据,雇佣和fire的费用相对高,工资超级低,这样的话,用我之前的方法有可能会总是fire或者雇佣,此时,可能一次性招募最多的人会省钱些。。。确实有可能。。。

另外,从这题得出的教训是,不管继续往后规划有没有用,第二种方法在得出同样的结果的前提下肯定要稳定些,这个显而易见,对整个雇佣区间都进行了规划取优。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[][];
int emp[];
int chire,cfire,cmonth;
int main()
{
int n;
while (scanf("%d",&n)&&n)
{
scanf("%d%d%d",&chire,&cmonth,&cfire);
int i,j,k;
int M=;
for (i=;i<=n;i++){
scanf("%d",&emp[i]);
if (M<emp[i]) M=emp[i];
}
for (j=;j<=n;j++)
for (k=emp[j];k<=M;k++){
if (j==)
dp[j][k]=k*(chire+cmonth);
else
dp[j][k]=<<;
}
for (i=;i<=n;i++){
for (j=emp[i-];j<=M;j++){
for (k=emp[i];k<=M;k++){
int temp=k*cmonth;
if (k>=j) temp+=(k-j)*chire;
else temp+=(j-k)*cfire;
dp[i][k]=min(dp[i][k],dp[i-][j]+temp);
}
}
}
int ans=<<;
for (i=emp[n];i<=M;i++)
if (ans>dp[n][i]) ans=dp[n][i];
printf("%d\n",ans);
}
}

ZOJ 1454 dp的更多相关文章

  1. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

  2. Deck of Cards ZOJ - 2852 dp 多决策 三维 滚动更新

    题意:一个特殊21点游戏 具体http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2852 题解:建一个三维dp,表示三个卡槽分别 ...

  3. ZOJ - 3632 DP 单调优化

    题意:买瓜,每天的瓜有不同的价格和xu命时间,要求能苟到第n天的最小代价 定义DP方程\(dp[i]\),指苟到第\(i\)天的最小代价,所求即为\(dp[n]\) 那么怎么转移就是问题,这里的状态表 ...

  4. ZOJ - 2402 DP方案数

    题意:给出m,序列第i位是第i-1位的至少2倍大,的求长度为n且每一位范围均在1-m的序列方案数 对求方案数做不到信手拈来的感觉,需要加强 用简单的预处理和最优子结构能优化到很不错的效率了 #incl ...

  5. zoj 3349 dp + 线段树优化

    题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...

  6. ZOJ 1276 DP

    给出一系列的1x2的矩阵,要你求出矩阵以什么样的次序相乘才使得相乘次数最少,.(不用排序,只要决定该矩阵是和前面相乘比较好,还是后面). 今天仔细想了一下,跟之前做的DP题目做了下对比,你比如说猴子堆 ...

  7. ZOJ 3735 dp

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3735 好久没做DP题了,一开始没理解题目里的C(M,3)是干什么,原来就是 ...

  8. ZOJ 3603 DP LCS

    已经5年没有做OJ了, 曾经沧海难为水,除去巫山不是云" 准备每周刷1-2题! 题目大意:给出N个字符串,且各个字符串都包含唯一的字母,即不存在"ABCA"(A重复了), ...

  9. ZOJ 3632 ----dp+优先队列

    上个礼拜学长讲了优先队列的说.... emmmmmm.... 看着题解敲了一题...先m下. #include<cstring> #include<algorithm> #in ...

随机推荐

  1. Educational Codeforces Round 65 选做

    好久没更博客了,随便水一篇 E. Range Deleting 题意 给你一个长度为 \(n\) 的序列 \(a_1,a_2,\dots a_n\) ,定义 \(f(l,r)\) 为删除 \(l\le ...

  2. Docker常用命令,Docker安装Nginx、Redis、Jenkins、tomcat、MySQL

    常用命令 拉取镜像:docker pull xxx启动镜像:docker run --name xxx 8080:8080 -d xxx查看容器:docker ps xxx 停止容器:docker s ...

  3. Thread start0 启动分析 一图看懂

    参考文章: https://segmentfault.com/a/1190000017255007 https://segmentfault.com/a/1190000020194154 1.线程启动 ...

  4. 【机器学习实战学习笔记(2-2)】决策树python3.6实现及简单应用

    文章目录 1.ID3及C4.5算法基础 1.1 计算香农熵 1.2 按照给定特征划分数据集 1.3 选择最优特征 1.4 多数表决实现 2.基于ID3.C4.5生成算法创建决策树 3.使用决策树进行分 ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring JDBCTemplate简介

    Spring 框架针对数据库开发中的应用提供了 JDBCTemplate 类,该类是 Spring 对 JDBC 支持的核心,它提供了所有对数据库操作功能的支持. Spring 框架提供的JDBC支持 ...

  6. springboot项目 线程消费队列注入报错误空指针

    背景: 在调用阿里云隐私保护有一个通话记录的回执消息是一个消费线程队列,这个还别人告诉我的,因为我根本没有看出来哪里是个线程了,然后我就把它当成普通的代码拿到返回值以后然后插入数据库 可是我这边该加的 ...

  7. 修改element-ui里table中悬浮框中三角号的颜色及透明度设置

    .el-tooltip__popper,.el-tooltip__popper.is-dark{background:rgba(0,0,0,0.6) !important;} .el-tooltip_ ...

  8. 第十五篇 用户认证auth

    用户认证auth 阅读目录(Content) 用户认证 auth模块 1 .authenticate() 2 .login(HttpRequest, user) 3 .logout(request) ...

  9. 洛谷 P5542 [USACO19FEB]Painting The Barn

    题目传送门 解题思路: 二维差分的板子题.题解传送门 AC代码: #include<iostream> #include<cstdio> using namespace std ...

  10. zabbix_server

    http://www.linuxidc.com/Linux/2014-11/109909.htm [root@localhost zabbix]# service iptables stop  关闭i ...