ICPC North Central NA Contest 2017 F. Atlantis —— 贪心

This way

题意:

现在有n个宝库,给你去每个宝库所需要的来回时间和海拔。海水高度一开始在0,每个单位时间增长1,你在来回某个宝库的时候,海水高度一定不能超过这个宝库的海拔。问你最多能访问多少宝库。

题解:

那么我们一定是优先访问海拔低的地方。但是我们怎么知道当时宝库是否访问呢,我们可以假设当前宝库会访问,用一个优先队列记录每个访问的宝库的时间,那么对于之后第i个宝库访问的时候,我们先将访问这个宝库的时间放入优先队列,然后如果总时间>这个宝库的海拔的话,就将访问时间最大的pop出去,这样就能保证用的时间是最少的。



#include <bits/stdc++.h>

#ifdef DEBUG

#   include "libs59/debugers.h"
//  #define debug(x)  cerr <<#x << " = "<<x<<endl;
#else
#   define endl '\n'
#   define debug(...)  
#endif

#define STOPSYNC ios::sync_with_stdio(false);cin.tie(nullptr)
#define MULTIKASE int Kase=0;cin>>Kase;for(int kase=1;kase<=Kase;kase++)
typedef long long ll;
const int MAXN = 2e5 + 59;
const int MOD = 1e9 + 7;
const int INF = 0x3F3F3F3F;
const ll llINF = 0x3F3F3F3F3F3F3F3F;
using namespace std;

ll fpow(ll a, ll b, ll mod = MOD) {
    ll ret = 1;
    while (b) {
        if (b & 1)ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

using PII=pair<int, int>;

void solve(int kaseId = -1) {
    int n;
    cin >> n;
    vector<PII> a(n);
    for (auto &ai:a) {
        int t, h;
        cin >> t >> h;
        ai = {h, t};
    }
    sort(a.begin(), a.end());
    priority_queue<int> q;
    int sum = 0;
    for (auto ai:a) {
        q.emplace(ai.second);
        sum += ai.second;
        while (sum > ai.first) {
            sum -= q.top();
            q.pop();
        }
    }
    cout << q.size() << endl;
}

void solves() {
    MULTIKASE {
        solve(kase);
    }
}

int main() {
    STOPSYNC;
    solve();
    return 0;
}
/*

 */
发布了554 篇原创文章 · 获赞 31 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/104600616