HDOJ 2037 今年暑假不AC
使用动态规划解题,直接将区间以开始点排序,然后将不重叠的点的最大值+1以记录就可以了。直接在循环中记录了最大点,更简洁。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //#include <fstream>
2 #include <memory>
3 #include <algorithm>
4 #include<iostream>
5 using namespace std;
6 struct Program
7 {
8 int start;
9 int end;
10 };
11 int cmp( const void *a , const void *b )
12 {
13 struct Program *c = (Program *)a;
14 struct Program *d = (Program *)b;
15 if(c->start >d->start) return 1;
16 else if(c->start <d->start) return -1;
17 else return 0;
18 }
19 int MaxLength(int len,int* traceLength,int* trace,Program* p)
20 {
21 qsort(p,len,sizeof(Program),cmp);
22 int max=0;
23 for(int i=1;i<len;i++)
24 {
25 for(int temp=0;temp<i;temp++)
26 {
27 if(p[i].start>=p[temp].end)
28 {
29 if(traceLength[i]<traceLength[temp])
30 {
31 trace[i]=temp;
32 traceLength[i]=traceLength[temp];
33 }
34 }
35 }
36 traceLength[i]++;
37 if(traceLength[i]>traceLength[max])
38 max=i;
39
40 }
41 return max;
42 }
43 int main()
44 {
45 //fstream cin("今年暑假不AC.txt");
46 int len=0;
47 while(cin>>len)
48 {
49 if(len==0)
50 break;
51 int* trace=new int[len];
52 int* traceLength=new int[len];
53 memset(traceLength,0,sizeof(int)*len);
54 traceLength[0]=1;
55 Program* program=new Program[len];
56 for(int i=0;i<len;i++)
57 cin>>program[i].start>>program[i].end;
58 int pos=MaxLength(len,traceLength,trace,program);
59 cout<<traceLength[pos]<<endl;
60 }
61 return 0;
62 }