[BZOJ1029][JSOI2007]建筑抢修(贪心+堆)
2017-05-03 18:49
288 查看
题目描述
传送门题目大意:修复每一个建筑都需要一定的时间,如果某一个建筑不能在某一个时刻前被修复就永不能被修复了,问最多能修复多少个建筑
题解
按照最晚的时刻排序,然后对于一个建筑,如果能修就修,否则让其替换前面一个耗时最长的建筑和工作安排那道题有点像
代码
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<queue> using namespace std; #define LL long long #define N 150005 int n,ans; struct data{LL x,y;}a ; LL sum; priority_queue <LL> q; int cmp(data a,data b){return a.y<b.y;} int main() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%lld%lld",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); for (int i=1;i<=n;++i) { if (sum+a[i].x<=a[i].y) sum+=a[i].x,q.push(a[i].x),++ans; else if (a[i].x<q.top()) { sum-=q.top(),q.pop(); sum+=a[i].x,q.push(a[i].x); } } printf("%d\n",ans); }
相关文章推荐
- bzoj 1029: [JSOI2007]建筑抢修(贪心+优先队列)
- bzoj 1029: [JSOI2007]建筑抢修 贪心+堆
- [BZOJ1029][JSOI2007]建筑抢修-堆-贪心
- 【BZOJ】1029: [JSOI2007]建筑抢修(贪心)
- BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]
- [bzoj1029][JSOI2007]建筑抢修【贪心】【堆】
- BZOJ 1029: [JSOI2007]建筑抢修 贪心
- 【BZOJ】1029 [JSOI2007]建筑抢修 贪心+堆
- bzoj 1029: [JSOI2007]建筑抢修【贪心+堆】
- [BZOJ 1029] [JSOI2007] 建筑抢修 【贪心】
- BZOJ 1029 JSOI2007 建筑抢修 贪心+堆
- BZOJ 1029: [JSOI2007]建筑抢修 堆,贪心,排序
- 【贪心+堆】BZOJ1029(JSOI2007)[建筑抢修]题解
- BZOJ 1029: [JSOI2007]建筑抢修【优先队列+贪心策略】
- 【bzoj1029】[JSOI2007]建筑抢修 贪心+堆
- BZOJ_1029_[JSOI2007]_建筑抢修_(贪心+优先队列)
- BZOJ 1029 [JSOI2007]建筑抢修 贪心
- BZOJ 1029: [JSOI2007]建筑抢修(贪心)
- [BZOJ1029] [JSOI2007]建筑抢修(贪心 + 优先队列)
- BZOJ1029 [JSOI2007]建筑抢修(贪心+堆)