Activity Selection Problem

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 /* greedy strategy:
 6  * 1. finish time small more and more
 7  * 2. every activity start time is small than finish time of pre activity
 8  */
 9 
10 
11 
12 
13 int main()
14 {
15 
16     int s[] =  {1, 3, 0, 5, 8, 5};
17     int f[] =  {2, 4, 6, 7, 9, 9};
18     int n = sizeof(s)/sizeof(s[0]);
19     sort(f,f+n);
20 
21     // from here, learn how to process two pointer just use one loop 
22     // rather than two loop
23     int i=0;
24     cout<<i<<endl;
25     for(int j=1;j<n;j++)
26     {
27         if(s[j]>f[i])
28         {
29             cout<<j<<endl;
30             i=j;
31         }
32     }
33     return 0;
34 }

 

上一篇:数据结构之堆排序、插入排序、希尔排序、快速排序


下一篇:Dispatch Group