hdu 4502 dp

吉哥系列故事——临时工计划

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3108    Accepted Submission(s): 1220


Problem Description
  俗话说一分钱难倒英雄汉,高中几年下来,吉哥已经深深明白了这个道理,因此,新年开始存储一年的个人资金已经成了习惯,不过自从大学之后他不好意思再向大人要压岁钱了,只能把唯一的希望放到自己身上。可是由于时间段的特殊性和自己能力的因素,只能找到些零零碎碎的工作,吉哥想知道怎么安排自己的假期才能获得最多的工资。
  已知吉哥一共有m天的假期,每天的编号从1到m,一共有n份可以做的工作,每份工作都知道起始时间s,终止时间e和对应的工资c,每份工作的起始和终止时间以天为单位(即天数编号),每份工作必须从起始时间做到终止时间才能得到总工资c,且不能存在时间重叠的工作。比如,第1天起始第2天结束的工作不能和第2天起始,第4天结束的工作一起被选定,因为第2天吉哥只能在一个地方工作。
  现在,吉哥想知道怎么安排才能在假期的m天内获得最大的工资数(第m+1天吉哥必须返回学校,m天以后起始或终止的工作是不能完成的)。
 
Input
第一行是数据的组数T;每组数据的第一行是2个正整数:假期时间m和可做的工作数n;接下来n行分别有3个正整数描述对应的n个工作的起始时间s,终止时间e,总工资c。

[Technical Specification]
1<=T<=1000
9<m<=100
0<n<=1000
s<=100, e<=100, s<=e
c<=10000
 
Output
对于每组数据,输出吉哥可获得的最高工资数。
 
Sample Input
1 10 5 1 5 100 3 10 10 5 10 100 1 4 2 6 12 266
 
Sample Output
102
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn=105;
 7 int dp[maxn];
 8 inline int max(int a,int b){return a>b?a:b;}
 9 struct node
10 {
11     int s,e,val;
12     bool operator <=(const node a)const{
13         if(e!=a.e) return e<=a.e;
14         else return s<=a.s;
15     }
16     bool operator >=(const node a)const{
17         if(e!=a.e) return e>=a.e;
18         else return s>=a.s;
19     }
20 }p[1005];
21 void swap(node &a,node &b){node t=a;a=b;b=t;}
22 void qsort(int l,int r)
23 {
24     if(l<r)
25     {
26         node t=p[l];
27         int i=l,j=r;
28         while(i!=j)
29         {
30             while(p[j]>=t && i<j) j--;
31             while(p[i]<=t && i<j) i++;
32             if(i<j) swap(p[i],p[j]);
33         }
34         p[l]=p[i];p[i]=t;
35         qsort(l,i-1);
36         qsort(i+1,r);
37     }
38 }
39 
40 int main()
41 {
42     int t,m,n,i,j;
43     scanf("%d",&t);
44     while(t--)
45     {
46         scanf("%d%d",&m,&n);
47         memset(dp,0,sizeof(dp));
48         for(i=1;i<=n;i++) scanf("%d%d%d",&p[i].s,&p[i].e,&p[i].val);
49         qsort(1,n);
50         //for(i=1;i<=n;i++) printf("%d %d %d
",p[i].s,p[i].e,p[i].val);
51         for(i=1;i<=m;i++)
52         for(j=1;j<=n;j++)
53         {
54             if(p[j].e>i) break;
55             dp[i]=max(dp[i],dp[p[j].s-1]+p[j].val);
56         }
57         printf("%d
",dp[m]);
58     }
59     return 0;
60 }
原文地址:https://www.cnblogs.com/xiong-/p/4100920.html