Codeforces Round 974 (Div. 3) G. Milky Days
题目
题解
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define lson p << 1
#define rson p << 1 | 1
const int N = 1e6, maxn = 1e6 + 5, inf = 1e13;
const int base = 137;
const __int128 mod = 212370440130137957LL;
// const int mod = 998244353;int n, m;
int a[maxn], b[maxn];void solve(){int res = 0;int k, h;int q;cin >> n >> m >> k;vector<int> d(n + 5);for(int i = 1; i <= n; i++){cin >> d[i] >> a[i];}d[n + 1] = inf;stack<pii> stk;int cur = 1;for(int i = 1; i <= n; i++){stk.push({d[i], a[i]});int need = m * (d[i + 1] - d[i]);//到下一个日期最多喝多少牛奶int sum = 0;//当前喝了多少牛奶int r = 0;//sum % mcur = d[i];//当前日期while(!stk.empty() && sum < need){auto [d2, a2] = stk.top();if(cur - d2 >= k) break;//过期了stk.pop();int ed = d2 + k - 1;a2 = min(a2, (ed - cur + 1) * m - r);if(sum + a2 >= need){a2 -= need - sum;sum = need;cur = d[i + 1];stk.push({d2, a2});}else{sum += a2;cur = d[i] + sum / m;r = sum % m;}}res += sum / m;}cout << res << '\n';
}signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int T = 1;cin >> T;while(T--){solve();}return 0;
}