QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112207 | #5415. Ropeway | mendicillin2 | WA | 3ms | 3472kb | C++17 | 2.8kb | 2023-06-10 16:44:55 | 2023-06-10 16:44:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define per(i, a, b) for (int i = int(b)-1; i >= int(a); i--)
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using ll = int64_t;
using vi = vector<int>;
using pii = pair<int, int>;
template <class T, class F> struct sliding_aggregation {
stack<T> as, bs, ae, be;
F f;
T one, vs, ve;
sliding_aggregation(F f_, T one_) : f(f_), one(one_), vs(one), ve(one) {}
void push_s(const T& x) {
as.push(x), bs.push(vs = f(x, vs));
}
void push_e(const T& x) {
ae.push(x), be.push(ve = f(ve, x));
}
void reduce() {
while (!ae.empty()) {
push_s(ae.top()), ae.pop();
}
while (!be.empty()) be.pop();
ve = one;
}
bool empty() const {
return as.empty() && ae.empty();
}
int size() const {
return int(as.size() + ae.size());
}
void push(const T& x) {
if (as.empty()) {
push_s(x), reduce();
} else {
push_e(x);
}
}
void pop() {
assert(!empty());
if (as.empty()) reduce();
as.pop(), bs.pop();
vs = (bs.empty() ? one : bs.top());
}
T query() const {
return f(vs, ve);
}
};
const ll INF = 1e17;
vector<ll> get_dp(int N, int K, const vector<ll>& A, const string& S) {
assert(int(A.size()) == N+2), assert(int(S.size()) == N+2);
vector<ll> dp(N+2);
auto imin = [](const ll& x, const ll& y) -> ll {
return min(x, y);
};
auto swag = sliding_aggregation(imin, INF);
swag.push(0);
for (int i = 1; i <= N+1; i++) {
while (swag.size() > K) swag.pop();
assert(!swag.empty());
dp[i] = swag.query() + A[i];
if (S[i] == '1') {
while (!swag.empty()) swag.pop();
}
swag.push(dp[i]);
}
return dp;
}
void solve() {
int N, K; cin >> N >> K;
vector<ll> A(N+2);
A[0] = A.back() = 0;
for (int i = 1; i <= N; i++) cin >> A[i];
string S; cin >> S;
S.insert(S.begin(), '1'), S.push_back('1');
auto fdp = get_dp(N, K, A, S);
auto bdp = get_dp(N, K, vector<ll>(A.rbegin(), A.rend()), string(S.rbegin(), S.rend()));
reverse(bdp.begin(), bdp.end());
int Q; cin >> Q;
vector<ll> rdp(K);
while (Q--) {
int p; ll v; cin >> p >> v;
ll res = INF;
if (S[p] == '1') {
res = fdp.back() - A[p] + v;
} else {
res = (fdp[p] - A[p]) + v + (bdp[p] - A[p]);
fill(rdp.begin(), rdp.end(), INF);
for (int k = 1; k < K; k++) {
rdp[k] = bdp[p+k];
if (S[p+k] == '1') break;
}
ll pmin = INF;
for (int k = 1; k < K; k++) {
pmin = min(pmin, fdp[p-k]);
res = min(res, pmin + rdp[K-k]);
if (S[p-k] == '1') break;
}
}
cout << res << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int T; cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3472kb
input:
3 10 3 5 10 7 100 4 3 12 5 100 1 0001000010 2 2 3 6 15 5 6 1 1 1 1 1 00000 1 3 100 5 6 1 1 1 1 1 00100 1 3 100
output:
206 214 0 100
result:
ok 4 number(s): "206 214 0 100"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3444kb
input:
500 19 6 285203460 203149294 175739375 211384407 323087820 336418462 114884618 61054702 243946442 19599175 51974474 285317523 222489944 26675167 300331960 1412281 324105264 33722550 169011266 1111111110110100011 18 3 127056246 5 100630226 14 301161052 2 331781882 5 218792226 2 190274295 12 49227476 ...
output:
2472886431 2299111966 2796055445 2650202148 2417273966 2508694561 2285479513 2584701013 2712540666 2240907690 2577284958 2826535456 2766700195 2511807344 2814193324 2438434986 2669077215 2682112324 498292963 729032050 211705888 564509290 715543137 492281467 639116182 18 19 19 19 20 21 54 1685424960 ...
result:
wrong answer 8th numbers differ - expected: '2521569560', found: '2584701013'