zoukankan      html  css  js  c++  java
  • ZOJ 1454 dp

    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[20][110];
    int emp[20];
    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=0;
            for (i=1;i<=n;i++){
                scanf("%d",&emp[i]);
                if (M<emp[i]) M=emp[i];
            }
            for (j=1;j<=n;j++)
             for (k=emp[j];k<=M;k++){
                if (j==1)
                dp[j][k]=k*(chire+cmonth);
                else
                    dp[j][k]=1<<30;
             }
            for (i=2;i<=n;i++){
               for (j=emp[i-1];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-1][j]+temp);
                }
               }
            }
            int ans=1<<30;
            for (i=emp[n];i<=M;i++)
                if (ans>dp[n][i]) ans=dp[n][i];
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    Vmware 虚拟硬盘 合并多个分割文件
    一步步带你做vue后台管理框架(三)——登录功能
    一步步带你做vue后台管理框架(二)——上手使用
    webpack教程(六)——分离组件代码
    webpack教程(五)——图片的加载
    webpack教程(四)——css的加载
    input输入框自动填充黄色背景解决方案
    webpack教程(三)——热刷新
    webpack教程(二)——webpack.config.js文件
    webpack教程(一)——初体验
  • 原文地址:https://www.cnblogs.com/kkrisen/p/3343460.html
Copyright © 2011-2022 走看看