UVa 1153 - Keep the Customer Satisfied (贪心 + 优先队列)

题意

输出能完成的最大任务数

思路

一开始写了二分 + DFS,果断TLE。

后来想了很久也没想到什么好办法,参考了别人的思路。

这思路也挺神奇的,用优先队列维护一个最大任务时间。

先排序 
如果加上curLast(当前任务持续时间)会超出curDeath,就pop掉队里的最大Last。

对此我想(ma)了(hou)想(pao)。如果curLast小于maxLast,显然把maxLast的那个任务不选,选择当前的这个任务较优。因为这样不仅能多放一个任务,还可以给后面腾出时间。

如果curLast > maxLast,那么不管替换还是不替换,不选择的任务数始终会增加一个。而且当后面任务来的时候总要被替换下来。

所以最后统计出的是被换下的任务数,用总数减一下就行。

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. #define BitCountll(x) __builtin_popcountll(x)
  31. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  32. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  33. const double PI = acos(-1.0);
  34. const int INF = 0x3f3f3f3f;
  35. using namespace std;
  36. const int MAXN = 800000 + 10;
  37. const int MOD = 1000007;
  38. typedef pair<int, int> pii;
  39. typedef vector<int>::iterator viti;
  40. typedef vector<pii>::iterator vitii;
  41. struct POINT
  42. {
  43. int last, death;
  44. bool operator < (const POINT &a) const
  45. {
  46. if (death != a.death) return death < a.death;
  47. return last < a.last;
  48. }
  49. }pit[MAXN];
  50. int n;
  51. priority_queue<int> pqu;
  52. int Solve()
  53. {
  54. int curTime = 0, ans = 0;
  55. for (int i = 0; i < n; i++)
  56. {
  57. curTime += pit[i].last;
  58. pqu.push(pit[i].last);
  59. if (curTime > pit[i].death)
  60. {
  61. curTime -= pqu.top();
  62. pqu.pop();
  63. ans++;
  64. }
  65. }
  66. return ans;
  67. }
  68. int main()
  69. {
  70. //ROP;
  71. int T, i, j;
  72. scanf("%d", &T);
  73. while (T--)
  74. {
  75. while (!pqu.empty()) pqu.pop();
  76. scanf("%d", &n);
  77. for (i = 0; i < n; i++) scanf("%d%d", &pit[i].last, &pit[i].death);
  78. sort(pit, pit + n);
  79. printf("%d\n", n - Solve());
  80. if (T) printf("\n");
  81. }
  82. return 0;
  83. }
 
 
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值