zoukankan      html  css  js  c++  java
  • Business

    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 (≤), 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
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 int dp[50][10000];
     6 struct node{
     7     int p,l,d;
     8 }a[55];
     9 int cmp(node a,node b){
    10     return a.d<b.d;    
    11 }
    12 int n;
    13 int imax(int a,int b){
    14     return a>b?a:b;
    15 }
    16 int main()
    17 {
    18     int i,j;
    19     int maxx=0;
    20     scanf("%d",&n);
    21     for(i=1;i<=n;i++){
    22         scanf("%d%d%d",&a[i].p,&a[i].l,&a[i].d);
    23         maxx=imax(maxx,a[i].d);
    24     }
    25     int maxn=0;
    26     sort(a+1,a+n+1,cmp);
    27     for(i=1;i<=n;i++)dp[i][a[i].l]=a[i].p;
    28     for(i=1;i<=n;i++){
    29         for(j=0;j<=maxx;j++){
    30             if(a[i].d>=j&&a[i].l<=j){
    31                 dp[i][j]=imax(dp[i-1][j-a[i].l]+a[i].p,dp[i-1][j]);
    32             }
    33             else if(a[i].d<j){
    34                 dp[i][j]=imax(dp[i-1][j],dp[i-1][a[i].d-a[i].l]+a[i].p);
    35             }
    36             else dp[i][j]=dp[i-1][j];
    37             maxn=imax(maxn,dp[i][j]);
    38         }
    39     }
    40    /* for(i=1;i<=n;i++){
    41         for(j=0;j<=maxx;j++){
    42             printf("%d %d:%d ",i,j,dp[i][j]);
    43             printf("
    ");
    44         }
    45     }*/
    46     printf("%d
    ",maxn);
    47     return 0;
    48 }
    诚者,君子之所守也。
  • 相关阅读:
    如何编写一个有效的缓存
    [深入Maven源代码]maven绑定命令行参数到具体插件
    java.util.ServiceLoader的用法
    非阻塞算法-栈
    非阻塞算法-简单的计数器
    Excelbatis-一个将excel文件读入成实体列表、将实体列表解析成excel文件的ORM框架,简洁易于配置、可扩展性好
    教你如何迭代地遍历二叉树
    [开源项目]Shell4Win,一个在Windows下执行shell命令的解释器
    [LeetCode]Single Number II
    CAS5.3服务环境搭建
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285825.html
Copyright © 2011-2022 走看看