1 条题解

  • 0
    @ 2026-11-2 13:31:35

    加油站与餐馆(Benzinska) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        long long D, X;
        if (!(cin >> n >> D >> X)) return 0;
        vector<pair<long long, long long>> st(n);
        for (int i = 0; i < n; ++i) cin >> st[i].first;
        for (int i = 0; i < n; ++i) cin >> st[i].second;
        sort(st.begin(), st.end());
        priority_queue<long long> pq;
        long long reach = D;
        int idx = 0, ans = 0;
        while (reach < X) {
            while (idx < n && st[idx].first <= reach) {
                pq.push(st[idx].second);
                ++idx;
            }
            if (pq.empty()) {
                cout << -1 << "\n";
                return 0;
            }
            reach += pq.top();
            pq.pop();
            ++ans;
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8674
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者