QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#115291 | #2356. Partition of Queries | ckiseki# | WA | 12ms | 14140kb | C++20 | 2.4kb | 2023-06-25 15:40:38 | 2023-06-25 15:40:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int cnt = sizeof...(T);
(..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
cerr << (f++ ? ", " : "") << *L;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
const int maxn = 1000025;
int pre[maxn], suf[maxn];
int64_t dp[maxn];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, y;
cin >> n >> y;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + (s[i] == '+');
}
for (int i = n - 1; i >= 0; i--) {
suf[i] = suf[i + 1] + (s[i] == '?');
}
dp[0] = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '+')
dp[0] += suf[i];
}
debug(dp[0]);
int64_t ans = dp[0];
deque<pair<int64_t,int64_t>> dq;
const auto insert = [&dq](int64_t slope, int64_t b) {
using pll = pair<int64_t,int64_t>;
const auto ok = [](pll A, pll B, pll C) {
assert (A.first <= B.first && B.first <= C.first);
return (C.second - B.second) * (B.first - A.first) > (B.second - A.second) * (C.first - B.first);
};
pair<int64_t,int64_t> cur(slope, b);
while (dq.size() >= 2 && !ok(dq[dq.size()-2], dq[dq.size()-1], cur))
dq.pop_back();
dq.emplace_back(cur);
};
const auto query = [&dq](int64_t x) {
const auto f = [](pair<int64_t,int64_t> l, int64_t z) {
return l.first * z + l.second;
};
while (dq.size() >= 2 && f(dq[0], x) >= f(dq[1], x))
dq.pop_front();
return f(dq[0], x);
};
insert(0, dp[0]);
for (int i = 1; i <= n; i++) {
dp[i] = 1e18;
dp[i] = query(suf[i - 1]) + y - pre[i] * suf[i - 1];
insert(pre[i], dp[i]);
// for (int j = 0; j < i; j++) {
// dp[i] = min(dp[i], dp[j] - (pre[i] - pre[j]) * suf[i - 1] + y);
// }
debug(i, dp[i]);
ans = min(ans, dp[i]);
}
cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3484kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3472kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3488kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
10 0 ++?+??++??
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3436kb
input:
12 100 +?+++??+??++
output:
19
result:
ok single line: '19'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3404kb
input:
1 1 ?
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
9 7 ++++++++?
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
9 8 ++++++++?
output:
8
result:
ok single line: '8'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3428kb
input:
10 15 ++++++++??
output:
15
result:
ok single line: '15'
Test #10:
score: 0
Accepted
time: 1ms
memory: 3396kb
input:
5 3 +?+?+
output:
3
result:
ok single line: '3'
Test #11:
score: 0
Accepted
time: 1ms
memory: 3404kb
input:
10 5 +?+?+??+??
output:
10
result:
ok single line: '10'
Test #12:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
10 7 +?+?+??+??
output:
12
result:
ok single line: '12'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
15 4 +?+?+??+??+??+?
output:
14
result:
ok single line: '14'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3404kb
input:
15 6 +?+?+??+??+??+?
output:
18
result:
ok single line: '18'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
19 8 +?+?+??+??+??+?++??
output:
28
result:
ok single line: '28'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3516kb
input:
20 9 +?+?+??+??+??+?++???
output:
30
result:
ok single line: '30'
Test #17:
score: 0
Accepted
time: 1ms
memory: 3408kb
input:
500 100 +?+?+??+??+??+?++?????++??+?+????????????+++?+?+???++?+?+++++++????++??+??+++??++++?++??+??+??+?????++++???+??++?+?++?++???++++???????+??????????++?+??+?+++???+?+???++?++?++++???++?++?????+??????+?++???++??+?++?+??+??++??++??++?+?+?+++??+??++????+?++?++?++??+?+++?+?+???+?+++?++?++++?????++++...
output:
2710
result:
ok single line: '2710'
Test #18:
score: 0
Accepted
time: 1ms
memory: 3664kb
input:
10000 100 +?+?+??+??+??+?++?????++??+?+????????????+++?+?+???++?+?+++++++????++??+??+++??++++?++??+??+??+?????++++???+??++?+?++?++???++++???????+??????????++?+??+?+++???+?+???++?++?++++???++?++?????+??????+?++???++??+?++?+??+??++??++??++?+?+?+++??+??++????+?++?++?++??+?+++?+?+???+?+++?++?++++?????++...
output:
56616
result:
ok single line: '56616'
Test #19:
score: -100
Wrong Answer
time: 12ms
memory: 14140kb
input:
500000 3000 +?+?+??+??+??+?++?????++??+?+????????????+++?+?+???++?+?+++++++????++??+??+++??++++?++??+??+??+?????++++???+??++?+?++?++???++++???????+??????????++?+??+?+++???+?+???++?++?++++???++?++?????+??????+?++???++??+?++?+??+??++??++??++?+?+?+++??+??++????+?++?++?++??+?+++?+?+???+?+++?++?++++?????...
output:
26942924642
result:
wrong answer 1st lines differ - expected: '17820759', found: '26942924642'