UVa 11776 - Oh Your Royal Greediness! (贪心 + 模拟)

题意

有N个农民要收割农田,给出一个开始时间和结束时间,问最少要派几个人去监督

思路

贪心一下,按开始时间从小到大排一下。

然后这时候就开始模拟了。

用一个优先队列维护可用的人,如果当前农民的开始时间比Q.top要大,说明空一个人,不用加人,否则ans++ 
最后这个人入队,入队的是结束的时间。

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <iomanip>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <string>
  15. #include <map>
  16. #include <cmath>
  17. using namespace std;
  18. #define LL long long
  19. #define ULL unsigned long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define X first
  26. #define Y second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. #define BitCountll(x) __builtin_popcountll(x)
  35. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  36. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  37. const double PI = acos(-1.0);
  38. const int INF = 0x3f3f3f3f;
  39. const double eps = 1e-8;
  40. const int MAXN = 1000 + 10;
  41. const int MOD = 1000007;
  42. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  43. int cases = 0;
  44. typedef pair<int, int> pii;
  45. typedef vector<int>::iterator viti;
  46. typedef vector<pii>::iterator vitii;
  47. pii evt[MAXN];
  48. priority_queue<int, vector<int>, greater<int> > Q;
  49. int main()
  50. {
  51. //ROP;
  52. int n, i, j;
  53. while (scanf("%d", &n), n != -1)
  54. {
  55. while (!Q.empty()) Q.pop();
  56. for (i = 0; i < n; i++)
  57. {
  58. int a, b;
  59. scanf("%d%d", &a, &b);
  60. evt[i] = {a, b};
  61. }
  62. sort(evt, evt + n);
  63. int ans = 0;
  64. for (i = 0; i < n; i++)
  65. {
  66. if (Q.empty()) ans++;
  67. else
  68. {
  69. int limit = Q.top();
  70. if (limit >= evt[i].X) ans++;
  71. else Q.pop();
  72. }
  73. Q.push(evt[i].Y);
  74. }
  75. printf("Case %d: %d\n", ++cases, ans);
  76. }
  77. return 0;
  78. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值