1. 程式人生 > 其它 >HDUOJ----4502吉哥系列故事——臨時工計劃

HDUOJ----4502吉哥系列故事——臨時工計劃

吉哥系列故事——臨時工計劃

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

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

Source

2013騰訊程式設計馬拉松初賽第〇場(3月20日)

程式碼:

用的是動態規劃的路線..

看下面的圖..

 1     #include<stdio.h>
 2     #include<string.h>
 3     #include<stdlib.h>
 4     #define maxn 1005
 5     struct Line
 6     {
 7         int st;
 8         int en;
 9         int val;
10         int buf;   //用來暫存資料
11     };
12     Line point[maxn];
13     int cmp(const void *a ,const void *b)
14     {
15         if((*(Line*)a).en==(*(Line*)b).en)
16           return (*(Line*)a).st - (*(Line*)b).st;
17       return (*(Line*)a).en - (*(Line*)b).en ;
18     }
19 
20     int main()
21     {
22         int test,m,n,i,cnt,j;
23         scanf("%d",&test);
24         while(test--)
25         {
26          scanf("%d%d",&m,&n);
27          memset(point,0,sizeof(point));
28          for(cnt=i=0;i<n;i++)
29          {
30            scanf("%d%d%d",&point[cnt].st,&point[cnt].en,&point[cnt].val);
31            point[cnt].buf=0;
32            if(point[cnt].en<=m)  //超過假期,就不放入陣列啦
33                cnt++;
34          }
35          //利用一次多級排序
36          qsort(point,cnt,sizeof(point[0]),cmp);
37          for( i=0 ; i<cnt ; i++ )
38          {  
39             for( j=i+1 ;j<cnt ;j++ )
40             {
41                 if( point[i].en < point[j].st &&point[j].buf < point[i].val+point[i].buf )
42                   point[j].buf = point[i].val+point[i].buf ;
43             }
44          }
45          int ans=0;
46          for(i=0;i<cnt;i++)
47          {
48              if(ans<point[i].buf+point[i].val) 
49                   ans=point[i].buf+point[i].val;
50          }
51          printf("%dn",ans);
52         }
53         return 0;
54     }