zoukankan      html  css  js  c++  java
  • PAT顶级 1002. Business (35)

    PAT顶级 1002. Business (35)


    As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the following:
    Project[1] takes 3 days, it must be finished in 3 days in order to gain 6 units of profit.
    Project[2] takes 2 days, it must be finished in 2 days in order to gain 3 units of profit.
    Project[3] takes 1 day only, it must be finished in 3 days in order to gain 4 units of profit.
    You may take Project[1] to gain 6 units of profit. But if you take Project[2] first, then you will have 1 day left to complete Project[3] just in time, and hence gain 7 units of profit in total. Notice that once you decide to work on a project, you have to do it from beginning to the end without any interruption.

    Input Specification


    Each input file contains one test case. For each case, the first line gives a positive integer N(<=50), and then followed by N lines of projects, each contains three numbers P, L, and D where P is the profit, L the lasting days of the project, and D the deadline. It is guaranteed that L is never more than D, and all the numbers are non-negative integers.

    Output Specification


    For each test case, output in a line the maximum profit you can gain.

    Sample Input


    4
    7 1 3
    10 2 3
    6 1 2
    5 1 1

    Sample Output


    18

    设dp[i][j]表示考虑到第i个项目,前一个项目结束时间为j的最大收益,那么可得DP方程为:
    dp[i][j+a[i].l]=max(dp[i][j+a[i].l],dp[k][j]) if(j+a[i].l<=a[i].d)
    由于没有说明数据范围,只能使用map表示二维dp数组了

    #include <map>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    struct node
    {
        int p,l,d;
        node() {}
        node(int tp,int tl,int td)
        {
            p=tp;
            l=tl;
            d=td;
        }
        bool operator < (const node &an) const
        {
            return d<an.d;
        }
    } a[55];
    typedef long long ll;
    map<ll,map<ll,ll> >dp;
    int main()
    {
        int n,p,l,d;
        while(cin>>n)
        {
            for(int i=1; i<=n; i++)
            {
                cin>>p>>l>>d;
                a[i]=node(p,l,d);
            }
            a[0]=node(0,0,0);
            sort(a+1,a+1+n);
            dp.clear();
            ll ans=0;
            for(int i=1; i<=n; i++)
            {
                for(int j=0; j<=a[i-1].d; j++)
                {
                    if(j+a[i].l<=a[i].d)
                    {
                        for(int k=0;k<i;k++){
                             dp[i][j+a[i].l]=max(dp[i][j+a[i].l],dp[k][j]+a[i].p);
                             ans=max(ans,dp[i][j+a[i].l]);
                        }
                    }
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    应用一:Vue之开发环境搭建
    基于vue项目的js工具方法汇总
    JavaScript 格式化数字、金额、千分位、保留几位小数、舍入舍去… 及其浮点数计算精度问题(推荐的类库 Numeral.js 和 accounting.js)
    Redis源码分析(二十五)--- zmalloc内存分配实现
    Redis源码分析(二十五)--- zmalloc内存分配实现
    SpringBoot系列——WebMvcConfigurer介绍
    跨域问题与SpringBoot解决方案
    源码分析SpringBoot启动
    SpringBoot+SpringSecurity+jwt整合及初体验
    【mysql学习】InnoDB数据结构
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/6936625.html
Copyright © 2011-2022 走看看