全部评论 3


  • 这个比较函数想实现"优先消耗获得最早的优惠票",但逻辑写错了。

    6天前 来自 浙江

    2

  • 当你找到合适的优惠票后,应该移除那张票,但你是pop_back(),这移除的是最后一张票,而不是你排序后找到的那张。

    6天前 来自 浙江

    2
  • 我喜欢用其他方法来做

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

    struct Ticket {
    int price; // 地铁票价
    int time; // 获得时间
    bool used; // 是否已使用
    };

    int main() {
    int n;
    cin >> n;

    long long total = 0;
    deque<Ticket> tickets;  // 存储优惠票
    
    for (int i = 0; i < n; i++) {
        int type, price, time;
        cin >> type >> price >> time;
        
        if (type == 0) {  // 地铁
            total += price;
            tickets.push_back({price, time, false});
        } else {  // 公交车
            // 清理过期票(超过45分钟)
            while (!tickets.empty() && time - tickets.front().time > 45) {
                tickets.pop_front();
            }
            
            // 寻找可用优惠票
            bool found = false;
            for (auto it = tickets.begin(); it != tickets.end(); it++) {
                if (!it->used && it->price >= price) {
                    // 找到可用票
                    it->used = true;
                    found = true;
                    break;
                }
            }
            
            if (!found) {
                total += price;
            }
        }
        
        // 定期清理已使用的票,避免队列过长
        // 注意:不能直接清理未过期的票,因为它们可能后面还有用
    }
    
    cout << total << endl;
    return 0;
    

    }

    6天前 来自 浙江

    1
暂无数据

提交答案之后,这里将显示提交结果~

首页