[JSOI2007] 建筑抢修 (贪心)

首先定义:

  • t i t_i ti 表示建筑为自爆的时间
  • a i a_i ai 表示修 i i i 要的时间

思考一些贪心策略?

直接按 t t t 贪心? 显然不行

我们可以考虑先按 t t t 贪心,中途再更改
t t t 从小到大排序后,中途轮流遍历每个建筑

如果中途某个建筑 i i i 无法再 t i t_i ti 的时间内修复,那么在先前选择修复的建筑中拿出 a j a_j aj 最大的 j j j 号建筑

a i &lt; a j a_i&lt;a_j ai<aj, 则放弃 j j j 转修 i i i

怎么证明?(显然

若第 i i i 号出现时间不足,那么前 i i i 个建筑最多修复 i − 1 i-1 i1

则我们必然选择 a i a_i ai 较小的前 i − 1 i-1 i1 个建筑,给后面的修复留下更多的时间

实现直接使用堆来维护选择的建筑中 a i a_i ai 最大的即可

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int T, n, ans ;

struct node {
	int w, t ;
} a[N] ;

pq <int> q ;

bool cmp(node a, node b) {
	return a.t < b.t ;
}

signed main(){
    scanf("%d", &n) ;
    rep(i, 1, n) scanf("%d%d", &a[i].w, &a[i].t) ;
    sort(a + 1, a + n + 1, cmp) ;
    rep(i, 1, n)
    if (T + a[i].w > a[i].t) {
    	if (a[i].w < q.top()) {
    		T -= q.top() ; q.pop() ;
    		q.push(a[i].w) ;
    		T += a[i].w ;
		}
	} else {
		q.push(a[i].w) ;
		ans++ ;
		T += a[i].w ;
	}
	printf("%d\n", ans) ;
	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值