1029: [JSOI2007]建筑抢修 贪心

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

题意:n个建筑,每个有修复时间和爆炸时间,没有在爆炸时间内修复就会爆炸,问最多能修复的建筑

题解:贪心,先把建筑按爆炸时间排序,维护一个优先队列,每次放入之前看能不能不爆炸,如果不会爆炸直接放入,否则找队列中最大修复时间和当前所需的修复时间比较,取更优者,最后队列大小就是答案了。

/**************************************************************
    Problem: 1029
    User: walfy
    Language: C++
    Result: Accepted
    Time:592 ms
    Memory:5176 kb
****************************************************************/
 
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
 
using namespace std;
 
const double g=10.0,eps=1e-12;
const int N=150000+10,maxn=123456+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
 
struct node{
    ll t1,t2;
    bool operator<(const node&rhs)const{
        return t1 < rhs.t1;
    }
}a[N];
bool cmp(const node &a, const node &b) {
    return a.t2 < b.t2;
}
priority_queue<node>q;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%lld%lld",&a[i].t1,&a[i].t2);
    sort(a, a + n, cmp);
    ll ans=0,num=0;
    for(int i = 0; i < n; i++)
    {
        if(ans+a[i].t1<=a[i].t2)q.push(a[i]),ans+=a[i].t1,num++;
        else
        {
            if(!q.empty()&&q.top().t1>a[i].t1)
            {
                ans+=-q.top().t1+a[i].t1;
                q.pop();
                q.push(a[i]);
            }
        }
    }
    printf("%lld\n",num);
    return 0;
}
/***********************
 
***********************/
View Code

猜你喜欢

转载自www.cnblogs.com/acjiumeng/p/8963216.html