#include<bits/stdc++.h> usingnamespace std; intmain(){ int N; longlong T; cin >> N >> T; vector<int> A(N); for (int i = 0; i < N; i++){ cin >> A[i]; } longlong S = 0; for (int i = 0; i < N; i++){ S += A[i]; } T %= S; for (int i = 0; i < N; i++){ if (T < A[i]){ cout << i + 1 << ' ' << T << endl; break; } T -= A[i]; } }
E. Least Elements
题意:有一个长度为 n 数组 a,有一个长度为 m 的窗口,初始框住 [1,m],每次右移一格直到 [n−m+1,n]。你需要对于每个位置回答,窗口内前 k 小的数之和。
#ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
int n, m, k; cin >> n >> m >> k;
vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; }
priority_queue<PII, vector<PII>, greater<PII>> big; priority_queue<PII> small; longlong ans = 0; for (int i = 0; i < m; i++) { if (i < k) { small.push({a[i], i}); ans += a[i]; } else { if (small.top().first <= a[i]) { big.push({a[i], i}); } else { auto u = small.top(); small.pop(); big.push(u); ans -= u.first; small.push({a[i], i}); ans += a[i]; } } }
vector<bool> vis(n); cout << ans << " "; for (int i = m; i < n; i++) { while (!big.empty() and vis[big.top().second]) big.pop(); while (!small.empty() and vis[small.top().second]) small.pop(); int t = i - m; // cout << i << endl; if (a[t] <= small.top().first) { ans -= a[t]; if (!big.empty() and big.top().first < a[i]) { auto u = big.top(); big.pop(); small.push(u); ans += u.first; big.push({a[i], i}); } else { small.push({a[i], i}); ans += a[i]; } } else { auto t = small.top(); if (t.first <= a[i]) { big.push({a[i], i}); } else { small.pop(); big.push(t); ans -= t.first; small.push({a[i], i}); ans += a[i]; } } vis[t] = true; cout << ans << " "; } cout << '\n'; return0; }