You've been asked to program a bot for a popular b...

Создано: 23 июня 2025 г.

Отвечено с использованием O3 от Chat01

Вопрос

You've been asked to program a bot for a popular bank that will automate the management of incoming requests. Every request has its own timestamp in seconds, and it is guaranteed that all requests come sequentially, i.e., the timestamp is strictly increasing. There are two types of incoming requests:

deposit <timestamp> <holder_id> <amount> — request to deposit <amount> amount of money in the <holder_id> account;
withdraw <timestamp> <holder_id> <amount> — request to withdraw <amount> amount of money from the <holder_id> account. As a bonus, the bank also provides a cashback policy — 2% of the withdrawn amount rounded down to the nearest integer will be returned to the account exactly 24 hours after the request timestamp. If the cashback and deposit/withdrawal happen at the same timestamp, assume cashback happens earlier.
Your system should also handle invalid requests. There are two types of invalid requests:

invalid account number;
withdrawal of a larger amount of money than is currently available.
For the given list of initial balances and requests, return the state of balances straight after the last request has been processed, or an array of a single element [-<request_id>] (please note the minus sign), where <request_id> is the 1-based index of the first invalid request. Note that cashback requests which haven't happened before the last request should be ignored.

Example

Example 1

For balances = [1000, 1500] and requests = ["withdraw 1613327630 2 480", "withdraw 1613327644 2 800", "withdraw 1614105244 1 100", "deposit 1614108844 2 200", "withdraw 1614108845 2 150"], the output should be solution(balances, requests) = [900, 295].

Explanation

Here are the states of balances after each request:

initially: [1000, 1500];
"withdraw 1613327630 2 480": [1000, 1020];
"withdraw 1613327644 2 800": [1000, 220];
At 1613414030 the 2nd account will receive the cashback of 480 * 0.02 = 9.6, which is rounded down to 9: [1000, 229];
At 1613414044 the 2nd account will receive the cashback of 800 * 0.02 = 16: [1000, 245];
After the next requests: "withdraw 1614105244 1 100": [900, 245], "deposit 1614108844 2 200": [900, 445], "withdraw 1614108845 2 150": [900, 295].

in c++ withotu comments

Мысль

Planning bot solution

I need to design the bot with C++ and simulate the behavior described. Starting with an initial balances array, I’ll handle two types of requests: deposits and withdrawals. Cashback (2%) needs to be processed 24 hours after a valid withdrawal and handled in timestamp order. I'll also need to manage the logic for invalid requests, like invalid accounts or withdrawals exceeding balance. The tricky part is ensuring timing and order correctly, especially for cashback with multiple requests at the same timestamp. I'll focus on a solution that keeps processing efficient.

Implementing request processing

I’m designing the bot to process requests in a sequential manner, handling deposits, withdrawals, and cashback. I’ll keep track of cashback events in a queue sorted by timestamp. Cashbacks occur 24 hours after withdrawals and may not always align with withdrawal order, so I’ll store them in a priority queue. For deposits and withdrawals, I'll first validate account IDs and balances before adjusting amounts. I need to ensure cashbacks are added correctly and efficiently, using integer amounts. Edge cases, like simultaneous cashback timestamps, should be manageable.

Calculating cashback and handling requests

I will focus on cashback calculations—taking integer division of amounts to avoid overflows using long long type for larger sums. The solution needs to handle requests (deposits and withdrawals) sequentially, with cashbacks processed before each request. I'll ensure edge cases, like coinciding timestamps for requests and cashback, are handled properly. After processing all requests, I’ll convert the result to a vector<int> as expected by the specification. However, I may internally use long long to ensure no overflow occurs during calculations. I will finalize the function with proper parsing and validation for each request.

Thought for 34 seconds

Ответ

cpp
#include <bits/stdc++.h> using namespace std; vector<int> solution(vector<int> balances, const vector<string>& requests) { using ll = long long; struct Cb { ll t; int id; ll v; bool operator>(const Cb& o) const { return t > o.t; } }; priority_queue<Cb, vector<Cb>, greater<Cb>> pq; const ll DAY = 86400; vector<ll> b(balances.begin(), balances.end()); for (size_t k = 0; k < requests.size(); ++k) { string op; ll ts; int id; ll amt; stringstream ss(requests[k]); ss >> op >> ts >> id >> amt; while (!pq.empty() && pq.top().t <= ts) { auto c = pq.top(); pq.pop(); b[c.id - 1] += c.v; } if (id < 1 || id > (int)b.size()) return { -(int)(k + 1) }; if (op == "deposit") { b[id - 1] += amt; } else { // withdraw if (b[id - 1] < amt) return { -(int)(k + 1) }; b[id - 1] -= amt; ll cb = (amt * 2) / 100; if (cb) pq.push({ ts + DAY, id, cb }); } } vector<int> res(b.size()); for (size_t i = 0; i < b.size(); ++i) res[i] = (int)b[i]; return res; }

Поделиться этим Q&A