can u optimize this code such that it doesnt take more than 2 seconds per testcase:…
- User
can u optimize this code such that it doesnt take more than 2 seconds per testcase: #include <bits/stdc++.h> #define int long long using namespace std;
signed main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> entries(n); for (auto &p : entries) { cin >> p.first >> p.second; } if (n == 0) { cout << "0\n"; continue; } int min_d = entries[0].first; int max_d = entries[0].first + k - 1; for (auto &[d, a] : entries) { max_d = max(max_d, d + k - 1); } deque<pair<int, int>> available; int cnt = 0; int i = 0; for (int day = min_d; day <= max_d; day++) { while (i < n && entries[i].first == day) { available.emplace_front(make_pair(entries[i].second, entries[i].first + k - 1)); i++; } while (!available.empty() && available.back().second < day) { available.pop_back(); } int remaining = m; while (remaining > 0 && !available.empty()) { if (available.front().first <= remaining) { remaining -= available.front().first; available.pop_front(); } else { available.front().first -= remaining; remaining = 0; } } if (remaining == 0) { cnt++; } } cout << cnt << "\n"; } }
Little John is as little as night is day — he was known to be a giant, at possibly 2.1 metres tall. It has everything to do with his love for milk.
His dairy diary has n entries, showing that he acquired ai pints of fresh milk on day di . Milk declines in freshness with time and stays drinkable for a maximum of k days. In other words, fresh milk acquired on day di will be drinkable between days di and di+k−1 inclusive.
Every day, Little John drinks drinkable milk, up to a maximum of m pints. In other words, if there are less than m pints of milk, he will drink them all and not be satisfied; if there are at least m pints of milk, he will drink exactly m pints and be satisfied, and it's a milk satisfaction day.
Little John always drinks the freshest drinkable milk first.
Determine the number of milk satisfaction days for Little John.
Input The first line of the input contains a single integer t (1≤t≤104 ), the number of test cases.
The first line of each test case consists of three integers n , m , k (1≤n , m , k≤105 ), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness.
Then follow n lines of each test case, each with two integers di and ai (1≤di , ai≤106 ), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of di , and all values of di are distinct.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
Output For each test case, output a single integer, the number of milk satisfaction days.
Example InputCopy 6 1 1 3 1 5 2 3 3 1 5 2 7 4 5 2 1 9 2 6 4 9 5 6 5 2 4 4 7 5 3 7 1 11 2 12 1 4 1 3 5 10 9 4 14 8 15 3 5 5 5 8 9 10 7 16 10 21 5 28 9 OutputCopy 3 3 4 5 10 6 Note In the first test case, 5 pints of milk are good for 3 days before spoiling.
- Assistant