题目1499:项目安排

来源:互联网 发布:苹果手机截动图软件 编辑:程序博客网 时间:2024/05/17 20:45
#include <stdio.h>#include <stdlib.h>#include <string.h> #define MAX 10001 typedef struct project{    int start;    int end;    int value;}Project; Project myproject[MAX];int dp[MAX];//状态dp[i]表示做完项目i后获得的收入 int compare(const void * p, const void * q){    Project * p1 = (Project *)p;    Project * q1 = (Project *)q;    return p1->end - q1->end;} int MaxValue(int n){    int i, j;    int start, end;    int max;    memset(dp, 0, sizeof(dp));    for (i = 1; i <= n; ++i){        start = myproject[i].start;        for (j = i - 1; j >= 0; --j){            if (start >= myproject[j].end && dp[i] < dp[j] + myproject[i].value){                dp[i] = dp[j] + myproject[i].value;            }        }    }    max = -1;    for (i = 1; i <= n; ++i)        if (max < dp[i])            max = dp[i];    return max;} int main(void){    int n, i;    while (scanf("%d", &n) != EOF){        for (i = 1; i <= n; ++i){            scanf("%d%d%d", &myproject[i].start, &myproject[i].end, &myproject[i].value);        }        myproject[0].start = 0;        myproject[0].end = 0;        myproject[0].value = 0;        qsort(myproject, n+1, sizeof(Project), compare);        printf("%d\n", MaxValue(n));    }     return 0;}/**************************************************************    Problem: 1499    User: cust123    Language: C++    Result: Accepted    Time:430 ms    Memory:1176 kb****************************************************************/

0 0