【BZOJ 1029】[JSOI2007]建筑抢修

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1029

【题意】

【题解】

/*
    按照T2升序排
    顺序枚举每一个建筑;
    如果当前建筑能够修理;
    则修理它;
    并将它的T1值加入到堆中;
    然后累加当前所用的时间now;
    如果加了这个T1之后会大于T2则这个建筑没办法修理;
    则在堆里面去找
    看看有没有比T1大的值;
    如果有的话,就改修这个建筑;而那个建筑不修了;
    这样虽然不能增加修理的建筑的个数;
    但是now的值会减小;
    增加了后面能够新修理建筑的机会.
    因为now是肯定小于等于T2的,
    所以更换以后,相当于是在确定的时间范围里面
        有两个建筑都能修好;
        但不能同时修好;
        则当然先修那个修的时间短的了。
    开long long安全点。
    用STL的priority_queue很方便。
*/


【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 15e4+100;

struct Node
{
    LL val;
    friend  bool operator<(Node a, Node b) { return  a.val < b.val; }
};

struct abc
{
    LL T1, T2;
};

priority_queue<Node>Q;
Node tq;
abc a[N];

int n,ans;
LL now;

void in()
{
    rei(n);
    rep1(i, 1, n)
    {
        rel(a[i].T1), rel(a[i].T2);
    }
}

bool cmp(abc a, abc b)
{
    return a.T2 < b.T2;
}

void ga()
{
    rep1(i, 1, n)
    {
        LL temp = now + a[i].T1;
        if (temp <= a[i].T2)
        {
            ans++;
            tq.val = a[i].T1;
            Q.push(tq);
            now = temp;
        }
        else
        {
            LL d = Q.top().val;
            if (d > a[i].T1)
            {
                Q.pop();
                tq.val = a[i].T1;
                Q.push(tq);
                now -= (d - a[i].T1);
            }
        }
    }
}

void out()
{
    printf("%d
", ans);
}

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    in();
    sort(a + 1, a + 1 + n, cmp);
    ga();
    out();
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626548.html