1622
This problem is done with dynamic programming, starting with the dp[], the array is labeled as time, but it takes a long, an exemplary code as follows
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5 #defineMAX 100026 #defineINF 999999997 structProject8 {9 intstart;Ten intend; One intvalue; A }; - - intcmpConst void*a,Const void*b) { theProject at = * (Project *) A; -Project BT = * (Project *) b; - returnAt.end-Bt.end; - } + - Project Act[max]; + intDp[inf]; A at intMainintargcChar Const*argv[]) - { - intN; - //freopen ("Input.txt", "R", stdin); - while(SCANF ("%d", &n)! =EOF) { - intEndmax =0; in for(inti =0; I < n; i++) { -scanf" %d%d%d", &act[i].start, &act[i].end, &act[i].value); to if(Act[i].end >Endmax) { +Endmax =Act[i].end; - } the } * //qsort (act,n,sizeof (Project), CMP); $ Panax NotoginsengMemset (DP,0,sizeof(DP)); - for(inti =0; I < n; i++) { the for(intj = Endmax; J >= Act[i].end; j--) { + inttemp = Dp[act[i].start] +Act[i].value; A if(Dp[j] <temp) { theDP[J] =temp; + } - } $ } $printf"%d\n", Dp[endmax]); - } - return 0; the}
Then the meaning of the subscript is changed to the task number, the code is as follows
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6 #defineMAX 100027 8 using namespacestd;9 structProjectTen { One intstart; A intend; - intvalue; - }; the - intcmpConst void*a,Const void*b) { -Project at = * (Project *) A; -Project BT = * (Project *) b; + returnAt.end-Bt.end; - } + A Project Act[max]; at intDp[max]; - - intMainintargcChar Const*argv[]) - { - intN; - //freopen ("Input.txt", "R", stdin); in while(SCANF ("%d", &n)! =EOF) { - intEndmax =0; to for(inti =0; I < n; i++) { +scanf" %d%d%d", &act[i].start, &act[i].end, &act[i].value); - if(Act[i].end >Endmax) { theEndmax =Act[i].end; * } $ }Panax NotoginsengQsort (Act,n,sizeof(Project), CMP); - theMemset (DP,0,sizeof(DP)); +dp[0] = act[0].value; A the for(inti =1; I < n; i++) { +Dp[i] = dp[i-1]; - for(intj = I1; J >=0; j--) { $ if(Act[j].end <=Act[i].start) { $Dp[i] = max (dp[i-1], Dp[j] +act[i].value); - Break; - } the } -Dp[i] =Max (Dp[i], act[i].value);Wuyi } theprintf"%d\n", dp[n-1]); - } Wu return 0; -}
The first code commits an error because there are no 43 lines in this sentence