1 条题解

  • 0
    @ 2025-5-11 21:17:22

    解题思路

    数据结构设计:

    使用结构体或类来表示物品(Item)、竞拍者(Bidder)和出价(Bid)。

    物品需要存储编号、最低价格和结束时间(可以转换为秒数以便比较)。

    竞拍者需要存储编号和账户余额。

    出价需要存储物品编号、竞拍者编号、出价金额和出价时间(转换为秒数)。

    处理输入:

    读取物品信息并存储,按结束时间排序。

    读取竞拍者信息并存储,使用哈希表或字典以便快速查找。

    读取出价信息并存储。

    处理拍卖:

    对于每个物品,筛选出所有出价时间不晚于物品结束时间的出价。

    在这些出价中,筛选出出价金额大于等于物品最低价格的出价。

    按出价金额从高到低排序,如果金额相同,按出价时间从早到晚排序。

    检查最高出价的竞拍者是否有足够的余额,如果有,则扣除金额并记录获胜者;否则检查下一个出价。

    输出结果:

    按物品结束时间的顺序输出结果。

    C++代码实现:

    #include <iostream>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <iomanip>
    #include <cstdlib> // for atoi
    
    using namespace std;
    
    struct Item {
        int id;
        double min_price;
        int end_time; // in seconds
    };
    
    struct Bidder {
        int id;
        double balance;
    };
    
    struct Bid {
        int item_id;
        int bidder_id;
        double amount;
        int time; // in seconds
    };
    
    // Helper function to convert time string to seconds
    int timeToSeconds(const string &time) {
        string h_str = time.substr(0, 2);
        string m_str = time.substr(3, 2);
        string s_str = time.substr(6, 2);
        int h = atoi(h_str.c_str());
        int m = atoi(m_str.c_str());
        int s = atoi(s_str.c_str());
        return h * 3600 + m * 60 + s;
    }
    
    bool compareItemsByTime(const Item &a, const Item &b) {
        return a.end_time < b.end_time;
    }
    
    bool compareBids(const Bid &a, const Bid &b) {
        if (a.amount != b.amount) {
            return a.amount > b.amount; // higher amount first
        }
        return a.time < b.time; // earlier time first if amounts are equal
    }
    
    int main() {
        int i, j, k;
        cin >> i;
        vector<Item> items(i);
        for (int n = 0; n < i; ++n) {
            string time;
            cin >> items[n].id >> items[n].min_price >> time;
            items[n].end_time = timeToSeconds(time);
        }
    
        // Sort items by end time
        sort(items.begin(), items.end(), compareItemsByTime);
    
        cin >> j;
        map<int, double> bidders; // bidder_id -> balance
        for (int n = 0; n < j; ++n) {
            int id;
            double balance;
            cin >> id >> balance;
            bidders[id] = balance;
        }
    
        cin >> k;
        vector<Bid> bids(k);
        for (int n = 0; n < k; ++n) {
            string time;
            cin >> bids[n].item_id >> bids[n].bidder_id >> bids[n].amount >> time;
            bids[n].time = timeToSeconds(time);
        }
    
        // Process each item in order of end time
        for (vector<Item>::const_iterator it = items.begin(); it != items.end(); ++it) {
            const Item &item = *it;
            vector<Bid> valid_bids;
            // Collect bids for this item that are before or at end time
            for (vector<Bid>::const_iterator bit = bids.begin(); bit != bids.end(); ++bit) {
                const Bid &bid = *bit;
                if (bid.item_id == item.id && bid.time <= item.end_time) {
                    valid_bids.push_back(bid);
                }
            }
            // Sort valid bids by amount (desc) and time (asc)
            sort(valid_bids.begin(), valid_bids.end(), compareBids);
    
            bool sold = false;
            for (vector<Bid>::const_iterator bit = valid_bids.begin(); bit != valid_bids.end(); ++bit) {
                const Bid &bid = *bit;
                if (bid.amount >= item.min_price && bidders[bid.bidder_id] >= bid.amount) {
                    // Winning bid found
                    cout << "Item " << item.id << " Bidder " << bid.bidder_id 
                         << " Price " << fixed << setprecision(2) << bid.amount << endl;
                    bidders[bid.bidder_id] -= bid.amount;
                    sold = true;
                    break;
                }
            }
            if (!sold) {
                cout << "Item " << item.id << " Reserve not met." << endl;
            }
        }
    
        return 0;
    }
    • 1

    信息

    ID
    1072
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者