bzoj1029: [JSOI2007]建筑抢修

15 篇文章 0 订阅

题面在这里

贪心是显然的吧。

一个想法就是考虑按结束时间排序,然后能修就修,不能修就不修。

但是明显是错的qwq。。因为你可以舍弃一些之前的来完成更多的任务。

所以我们应该是舍弃掉之前做过时间最长的,正确性显然。

然后用堆维护一下即可。


/*************************************************************
	Problem: bzoj 1029 [JSOI2007]建筑抢修
	User: fengyuan
	Language: C++
	Result: Accepted
	Time: 492 ms
	Memory: 2380 kb
	Submit_Time: 2017-11-16 21:52:48
*************************************************************/

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int N = 150010;
int n, time, ans;

struct Node{
	int t, ed;
}a[N];

const bool cmp(const Node &x, const Node &y)
{
	return x.ed < y.ed;
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++){
		scanf("%d%d", &a[i].t, &a[i].ed);
	}
	sort(a+1, a+1+n, cmp);
	priority_queue<int> q;
	time = 0; ans = 0;
	for (int i = 1; i <= n; i ++){
		time += a[i].t;
		q.push(a[i].t);
		ans ++;
		if (time > a[i].ed){
			time -= q.top();
			q.pop();
			ans --;
		}
	}
	printf("%d\n", ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值