1029: [JSOI2007]建筑抢修

Time Limit: 4 Sec  Memory Limit: 162 MB
Submit: 5452  Solved: 2422
[Submit][Status][Discuss]

Description

  小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的
入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
的建筑。

Input

  第一行是一个整数N接下来N行每行两个整数T1,T2描述一个建筑:修理这个建筑需要T1秒,如果在T2秒之内还
没有修理完成,这个建筑就报废了。

Output

  输出一个整数S,表示最多可以抢修S个建筑.N < 150,000;  T1 < T2 < maxlongint

Sample Input

4
100 200
200 1300
1000 1250
2000 3200

Sample Output

3

HINT

 

Source

析:先对 t2 从小到大排序,然后再维护一个优先队列,t1 大的优先,然后每次在考虑 a[i] 时,如果能完成就直接放入队列,否则和队列首部的那个元素比较,如果完成时间比队列首部的时间要小,那么删除首部那个,插入这个,否则直接丢弃,最后队列中元素个数就是答案。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 150000 + 10;
const int maxm = 3e5 + 10;
const int mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
int t1, t2;
bool operator < (const Node &p) const{
return t1 < p.t1 || t1 == p.t1 && t2 > p.t2;
}
};
Node a[maxn];
inline bool cmp(const Node &lhs, const Node &rhs){
return lhs.t2 < rhs.t2;
} int main(){
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%d %d", &a[i].t1, &a[i].t2);
sort(a, a + n, cmp);
priority_queue<Node> pq;
int l = 0;
for(int i = 0; i < n; ++i){
if(l + a[i].t1 <= a[i].t2) pq.push(a[i]), l += a[i].t1;
else if(pq.top().t1 > a[i].t1){
l += a[i].t1 - pq.top().t1;
pq.pop(); pq.push(a[i]);
}
}
printf("%d\n", (int)pq.size());
return 0;
}

  

BZOJ 1029 [JSOI2007]建筑抢修 (贪心 + 优先队列)的更多相关文章

  1. BZOJ 1029: [JSOI2007]建筑抢修【优先队列+贪心策略】

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 4810  Solved: 2160[Submit][Statu ...

  2. BZOJ 1029: [JSOI2007]建筑抢修 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1029 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落 ...

  3. BZOJ 1029 JSOI2007 建筑抢修 贪心+堆

    题目大意:n个建筑须要抢修.第i个建筑须要T1时间抢修.必须在T2时间之前抢修完成.求最多能抢修多少建筑 首先我们对T2排序 然后依次修理 可是这样贪心显然是不对的 比方说这组数据: 5 10 10 ...

  4. bzoj 1029 [JSOI2007]建筑抢修——贪心(伪dp)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1029 当然要按结束时间排序,然后按顺序修或跳过.就是那种“……不会使答案不优”的证明. 想了 ...

  5. BZOJ 1029: [JSOI2007]建筑抢修 堆+贪心

    1029: [JSOI2007]建筑抢修 Description 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有 ...

  6. BZOJ 1029 [JSOI2007] 建筑抢修(贪心)

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 2285  Solved: 1004[Submit][Statu ...

  7. BZOJ 1029: [JSOI2007]建筑抢修 优先队列

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  8. BZOJ 1029 [JSOI2007]建筑抢修 已更新

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 2748  Solved: 1213[Submit][Statu ...

  9. BZOJ 1029: [JSOI2007]建筑抢修

    1029: [JSOI2007]建筑抢修 Description 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有 ...

随机推荐

  1. echarts故障统计多维柱状图 堆叠柱状图 柱状图Demo2

    黑底:echarts链接:http://gallery.echartsjs.com/editor.html?c=xnP8JPeu4R option = { backgroundColor: 'blac ...

  2. Oauth2.0 认证的Web api例子

    Oauth2.0的解释 OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容.OA ...

  3. 使用gearman进行异步的邮件或短信发送

    一.准备工作 1.为了防止,处理业务途中出现的宕机,请配置好gearman的持久化方式.2.使用gearmanManager来管理我们的worker脚本,方便测试. 上述两条请看我之前写的两篇文章 二 ...

  4. System.ServiceProcess.TimeoutException: Time out has expired and the operation has not been completed.

    项目代码如下 ServiceController service = new ServiceController("ModbusAgent"); service.Stop(); T ...

  5. VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战(原创)

    VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战 1.给指定项目安装Newtonsoft.Json ,Version 4.5.11 PM> Install-Package Ne ...

  6. C#做一个写txt文件流的测试,为什么配置低的机器写入的还快

    测试机:笔记本i7 8G 固态硬盘 由于采取读码写入txt方式, 读码频率挺高,文件名为日期格式,当前采用每次读码打开文件写入的方式, 为什么没用sb,因为怕断电情况的数据丢失.所以采取每条存入的方式 ...

  7. c# 线程启动while(true) 死循环,里边的return导致线程退出情况,查错

    写了一个线程 线程下启动了一个循环 while(true) { 里边有个判断 如果为空不操作, 有余这个线程是后加的,老程序里边因为有个return没关注,导致线程退出而不能不听的监控 } 线程启动一 ...

  8. andorid 单选与复选

    activity_ui1.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  9. NC 6系预警类型注册

    在实际开发预警任务中,因为模块是新创建的,所以开发预警,就要在相应的节点模块注册.但这样代码就得放在相应的模块中,注册个预警类型,就可以把代码直接放在自己新建的模块. .先执行新建模块语句 inser ...

  10. java如何实现发邮件功能。

    http://blog.sina.com.cn/s/blog_59ca2c2a01013800.html http://www.cnblogs.com/xdp-gacl/p/4216311.html