【JSOI 2007】建筑抢修

【题目链接】

          点击打开链接

【算法】

           将T2从小到大排序,当决策当前建筑修或不修时,若当前花费时间 + T1 <= T2,则修,否则判断T1是否小于之前修的

           T1最大的建筑,若小于,则修,我们可以用一个大根堆来维护T1的最大值

           这题用的其实就是贪心的思想 : 从局部最优到全局最优

【代码】

          

#include<bits/stdc++.h>
using namespace std;
#define MAXN 150000

struct info {
        int T1,T2;
} a[MAXN+10];

int N,i,ans,s;
priority_queue<int> q;

template <typename T> inline void read(T &x) {
        int f=1; x=0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
        for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
        x *= f;
}

template <typename T> inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x/10);
    putchar(x%10+'0');
}

template <typename T> inline void writeln(T x) {
    write(x);
    puts("");
}

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

int main() {
        
        read(N);
        for (i = 1; i <= N; i++) {
                read(a[i].T1);
                read(a[i].T2);
        } 
        sort(a+1,a+N+1,cmp);
        
        for (i = 1; i <= N; i++) {
                if (s + a[i].T1 <= a[i].T2) {
                        s += a[i].T1;
                        q.push(a[i].T1);
                        ++ans;
                }    else if ((!q.empty()) && (a[i].T1 < q.top())) {
                        s = s - q.top() + a[i].T1;
                        q.pop();
                        q.push(a[i].T1);
                }
        }
        
        writeln(ans);
        
        return 0;
    
}

           

原文地址:https://www.cnblogs.com/evenbao/p/9196413.html