zoukankan      html  css  js  c++  java
  • BZOJ 1029: [JSOI2007]建筑抢修( 贪心 )

    按t2排序然后贪心...

    ----------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
     
    using namespace std;
     
    const int maxn = 150009;
     
    struct O {
    int t1, t2;
    void Read() {
    scanf("%d%d", &t1, &t2);
    }
    bool operator < (const O &o) const {
    return t2 < o.t2;
    }
    } o[maxn];
     
    int N;
    priority_queue<int> q;
     
    void init() {
    scanf("%d", &N);
    for(int i = 0; i < N; i++) o[i].Read();
    }
     
    void solve() {
    sort(o, o + N);
    int cnt = 0, ans = 0;
    for(int i = 0; i < N; i++) {
    if(cnt + o[i].t1 <= o[i].t2)
    cnt += o[i].t1, q.push(o[i].t1), ans++;
    else {
    int x = q.top();
    if(x > o[i].t1 && cnt - x + o[i].t1 <= o[i].t2)
    q.pop(), q.push(o[i].t1), cnt += o[i].t1 - x;
    }
    }
    printf("%d ", ans);
    }
    int main() {
    init();
    solve();
    return 0;
    }

    --------------------------------------------------------------------------- 

    1029: [JSOI2007]建筑抢修

    Time Limit: 4 Sec  Memory Limit: 162 MB
    Submit: 3033  Solved: 1355
    [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

  • 相关阅读:
    Python之图片格式转换
    pip依赖安装与记录
    Spectral Graph Theory的一些定理
    Beamer加中文
    Python之json
    Windows之建立C++开发环境
    Mysql分表教程
    null和空 not null
    yii 隐藏index.php的步骤
    yii泛域名
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4929791.html
Copyright © 2011-2022 走看看