QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#133146 | #5247. Walizki [C] | jrjyy | 0 | 1ms | 3584kb | C++20 | 3.5kb | 2023-08-01 16:24:19 | 2023-08-01 16:24:28 |
Judging History
answer
/* qoj_p5247.cpp */
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, k; std::cin >> n >> k;
std::string s; std::cin >> s;
std::vector<int> stk, b(n, n), id(n, -1); int cur = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '(') {
stk.push_back(i);
} else {
if (!stk.empty()) b[i] = stk.back(), b[stk.back()] = i, stk.pop_back();
else b[i] = -1;
}
}
for (int i = 0; i < n; ++i) if (s[i] == '(' && b[i] < n && id[i] == -1) {
for (int j = i; s[j] == '(' && j < n; j = b[j] + 1) id[j] = id[b[j]] = cur;
++cur;
}
// for (int i = 0; i < n; ++i) std::cerr << b[i] << ' ' << id[i] << '\n';
auto calc = [&](int ql, int qr) {
static int l = 0, r = 0; static i64 x = 0;
static std::vector<int> f(cur);
while (r < qr) x += s[r] == ')' && b[r] >= l ? ++f[id[r]] : 0, ++r;
while (l > ql) --l, x += s[l] == '(' && b[l] < r ? ++f[id[l]] : 0;
while (qr < r) --r, x -= s[r] == ')' && b[r] >= l ? f[id[r]]-- : 0;
while (ql > l) x -= s[l] == '(' && b[l] < r ? f[id[l]]-- : 0, ++l;
// assert(x == (qr - ql == 2 ? 1 : 0)); //
// i64 y = 0;
// for (int i = ql; i < qr; ++i) for (int j = i + 2; j <= qr; j += 2) {
// int x = 0; bool f = true;
// for (int k = i; k < j; ++k) x += (s[k] == '(' ? 1 : -1), f &= x >= 0;
// y += f && x == 0;
// }
// std::cerr << "Move " << l << ' ' << r << '\n';
// if (y != x) {
// std::cerr << "X" << x << ' ' << y << '\n';
// for (auto &x : f) std::cerr << x << ' ';
// }
// assert(y == x);
return x;
};
using Info = std::pair<i64, int>;
auto merge = [&](const Info &x, const Info &y) {
return Info{x.first + y.first, x.second + y.second};
};
auto get = [&](auto val) {
std::vector<Info> f(n + 1, Info{1e18, -1}); f[0] = Info{0, 0};
auto trans = [&](auto self, int l, int r, int ql, int qr) -> void {
if (l >= r) return;
int m = (l + r) / 2; std::pair<Info, int> x{Info{1e18, -1}, -1};
assert(ql < qr);
for (int i = ql; i < qr; ++i)
x = std::min(x, {merge(f[i], Info{calc(i, m + 1) - val, 1}), i});
f[m + 1] = std::min(f[m + 1], x.first);
// std::cerr << "Got " << m + 1 << ' ' << x.second << ": "
// << x.first.first << ' ' << x.first.second << '\n';
self(self, l, m, ql, x.second + 1), self(self, m + 1, r, x.second, qr);
};
auto solve = [&](auto self, int l, int r) -> void {
if (l >= r) return;
int m = (l + r) / 2;
if (r - l > 1) self(self, l, m);
// std::cerr << "Trans " << l << ' ' << m + 1
// << " -> " << m + 1 << ' ' << r + 1 << '\n';
trans(trans, m, r, l, m + 1);
if (r - l > 1) self(self, m, r);
};
solve(solve, 0, n);
return f[n];
};
i64 lo = -1e10, hi = 0;
while (lo < hi) {
i64 m = (lo + hi + 1) / 2;
auto [s, c] = get(m);
// std::cerr << m << ": " << c << ' ' << s << '\n';
if (c <= k) lo = m;
else hi = m - 1;
// std::cerr << lo << ' ' << hi << '\n';
}
auto [ans, c] = get(lo); ans += lo * k;
std::cout << ans << '\n';
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3584kb
input:
1 0
output:
10000000000
result:
wrong answer 1st lines differ - expected: '1', found: '10000000000'
Subtask #2:
score: 0
Wrong Answer
Test #12:
score: 0
Wrong Answer
time: 1ms
memory: 3464kb
input:
2 1 2 0
output:
0
result:
wrong answer 1st lines differ - expected: '1', found: '0'
Subtask #3:
score: 0
Wrong Answer
Test #23:
score: 0
Wrong Answer
time: 0ms
memory: 3528kb
input:
11 10 2 3 4 5 6 7 8 9 10 11 9 3 4 5 6 7 8 9 10 11 8 4 5 6 7 8 9 10 11 7 5 6 7 8 9 10 11 6 6 7 8 9 10 11 5 7 8 9 10 11 4 8 9 10 11 3 9 10 11 2 10 11 1 11 0
output:
0
result:
wrong answer 1st lines differ - expected: '2520', found: '0'
Subtask #4:
score: 0
Wrong Answer
Test #34:
score: 0
Wrong Answer
time: 1ms
memory: 3536kb
input:
12 11 2 3 4 5 6 7 8 9 10 11 12 10 3 4 5 6 7 8 9 10 11 12 9 4 5 6 7 8 9 10 11 12 8 5 6 7 8 9 10 11 12 7 6 7 8 9 10 11 12 6 7 8 9 10 11 12 5 8 9 10 11 12 4 9 10 11 12 3 10 11 12 2 11 12 1 12 0
output:
0
result:
wrong answer 1st lines differ - expected: '27720', found: '0'
Subtask #5:
score: 0
Wrong Answer
Test #45:
score: 0
Wrong Answer
time: 0ms
memory: 3512kb
input:
25 24 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 23 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 22 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 21 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 20 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
output:
0
result:
wrong answer 1st lines differ - expected: '5354228880', found: '0'
Subtask #6:
score: 0
Wrong Answer
Test #56:
score: 0
Wrong Answer
time: 1ms
memory: 3532kb
input:
26 25 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 24 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 23 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 22 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 21 6 7 8 9 10 11 12 13 14 15 ...
output:
0
result:
wrong answer 1st lines differ - expected: '26771144400', found: '0'
Subtask #7:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 1ms
memory: 3584kb
input:
61 60 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 59 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 ...
output:
0
result:
wrong answer 1st lines differ - expected: '9690712164777231700912800', found: '0'
Subtask #8:
score: 0
Wrong Answer
Test #78:
score: 0
Wrong Answer
time: 0ms
memory: 3500kb
input:
62 61 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 60 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 ...
output:
0
result:
wrong answer 1st lines differ - expected: '591133442051411133755680800', found: '0'
Subtask #9:
score: 0
Wrong Answer
Test #89:
score: 0
Wrong Answer
time: 1ms
memory: 3468kb
input:
91 90 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 89 3 4 5 6 7 8 9 10 11 12 13 14 ...
output:
0
result:
wrong answer 1st lines differ - expected: '718766754945489455304472257065075294400', found: '0'
Subtask #10:
score: 0
Wrong Answer
Test #101:
score: 0
Wrong Answer
time: 1ms
memory: 3460kb
input:
92 91 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 90 3 4 5 6 7 8 9 10 11 12 13 ...
output:
0
result:
wrong answer 1st lines differ - expected: '718766754945489455304472257065075294400', found: '0'