当前位置:   article > 正文

事件序列问题 贪心法_贪心,最多事件问题

贪心,最多事件问题
  1. /* 事件序列问题 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <algorithm>
  7. using namespace std;
  8. typedef struct event {
  9. int no, bgn, en;
  10. } event_t;
  11. bool cmp(const event_t e1, const event_t e2) {
  12. return e1.en < e2.en;
  13. }
  14. const int N = 12;
  15. event_t arr[N];
  16. int res[N];
  17. int main() {
  18. FILE * fin = fopen("in.txt", "r");
  19. for (int i = 0; i < 12; i++) {
  20. arr[i].no = i;
  21. fscanf(fin, "%d %d", &arr[i].bgn, &arr[i].en);
  22. }
  23. sort(arr, arr + N, cmp);
  24. int i = 0, index = 0, startTime = 0;
  25. while (i < N) {
  26. if (arr[i].bgn >= startTime) {
  27. res[index++] = arr[i].no;
  28. startTime = arr[i].en;
  29. }
  30. i++;
  31. }
  32. for (int j = 0; j < index; j++)
  33. printf("%d ", res[j]);
  34. putchar('\n');
  35. fclose(fin);
  36. return 0;
  37. }
  38. /*
  39. 1 3
  40. 3 4
  41. 0 7
  42. 3 8
  43. 2 9
  44. 5 10
  45. 6 12
  46. 4 14
  47. 10 15
  48. 8 18
  49. 15 19
  50. 15 20
  51. */

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号