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

1029: [JSOI2007]建筑抢修

Time Limit: 4 Sec  Memory Limit: 162 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1029

Description

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

Input

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

Output

输出一个整数S,表示最多可以抢修S个建筑。 数据范围: N<150000,T1

Sample Input

4
100 200
200 1300
1000 1250
2000 3200

Sample Output

3

HINT

题解:

首先按照结束时间进行排序

用一个大根堆,把每次可行的事情都扔进去,如果遇到一件事儿不行的时候,那么我们就把这件事情所花费的时间与大根堆的第一个进行对比

如果比他消耗时间小的话,那么必然可以替代那件事儿

证明还是挺简单的,想想就清楚了

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff;   //无限大
const int inf=0x3f3f3f3f;
/*

*/
//**************************************************************************************

inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

struct node
{
    int x,y;
};
bool cmp(node a,node b)
{
    return a.y<b.y;
}
node a[maxn];
priority_queue<int> q;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        a[i].x=read(),a[i].y=read();
    sort(a,a+n,cmp);
    int now=0;
    int ans=0;
    for(int i=0;i<n;i++)
    {
        if(now+a[i].x<=a[i].y)
        {
            ans++;
            now+=a[i].x;
            q.push(a[i].x);
        }
        else
        {
            int h=q.top();
            if(a[i].x<h)
            {
                q.pop();
                q.push(a[i].x);
                now=now+a[i].x-h;
            }
        }
    }
    cout<<ans<<endl;
}

 

 

 

posted @ 2015-03-31 18:41  qscqesze  阅读(417)  评论(0编辑  收藏  举报