fork(13) download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int st[1000], en[1000];
  7. int dp[1000][1000];
  8.  
  9. int solve(int index, int currentFinishTime){
  10. if(index == n) return 0;
  11. int v1 = 0, v2 = 0;
  12. if(dp[index][currentFinishTime] != -1) return dp[index][currentFinishTime];
  13.  
  14. //do not choose the current activity
  15. v1 = solve(index+1, currentFinishTime);
  16.  
  17. //try to choose the current activity
  18. if(st[index] >= currentFinishTime){
  19. v2 = solve(index+1, en[index]) + 1;
  20. }
  21. return dp[index][currentFinishTime] = max(v1, v2);
  22. }
  23.  
  24. int main(){
  25. cin >> n;
  26. for(int i = 0;i < n;i++) cin >> st[i] >> en[i];
  27. memset(dp, -1, sizeof dp);
  28.  
  29. cout << solve(0, 0) << endl;
  30. return 0;
  31. }
  32.  
  33. /*
  34. 11
  35. 1 4
  36. 3 5
  37. 0 6
  38. 5 7
  39. 3 9
  40. 5 9
  41. 6 10
  42. 8 11
  43. 8 12
  44. 2 14
  45. 12 16
  46. */
Success #stdin #stdout 0s 7372KB
stdin
11
1 4
3 5
0 6
5 7
3 9
5 9
6 10
8 11
8 12
2 14
12 16
stdout
4