QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#22473 | #2356. Partition of Queries | li0201# | WA | 5ms | 15708kb | C++20 | 1.8kb | 2022-03-09 18:59:18 | 2022-04-30 01:10:17 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define ROF(i, a, b) for (int i = a; i >= b; --i)
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int N = 1e6 + 7;
using namespace std;
int n;
ll f[N], g[N], W;
char ch[N];
ll w[N], s[N], c[N];
int q[N], hd, tl;
ll X(int i) { return w[i]; }
ll Y(int i) { return g[i]; }
double slope(int i, int j) { return X(i) == X(j) ? 1e18 : (Y(i) - Y(j)) / (X(i) - X(j)); }
int main() {
n = read(), W = read();
scanf("%s", ch + 1);
FOR(i, 1, n) {
w[i] = w[i - 1] + (ch[i] == '+');
c[i] = c[i - 1] + (ch[i] == '?');
}
FOR(i, 1, n) { s[i] = s[i - 1] + w[i] * (ch[i] == '?'); }
memset(f, 0x3f, sizeof(f));
f[0] = 0;
// FOR(i, 1, n) {
// int id = 0;
// FOR(j, 0, i - 1) {
// if (g[j] - c[i] * w[j] + s[i] + W < f[i]) {
// f[i] = g[j] - c[i] * w[j] + s[i] + W;
// id = j;
// }
// }
// printf("%d %d %lld\n", i, id, f[i]);
// g[i] = f[i] - s[i] + c[i] * w[i];
// }
q[hd = tl = 1] = 0;
FOR(i, 1, n) {
while (hd < tl && slope(q[hd], q[hd + 1]) < c[i]) ++hd;
f[i] = g[q[hd]] - c[i] * w[q[hd]] + s[i] + W;
g[i] = f[i] - s[i] + c[i] * w[i];
while (hd < tl && slope(q[tl - 1], i) < slope(q[tl - 1], q[tl])) --tl;
q[++tl] = i;
}
printf("%lld\n", f[n] - W);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 15704kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 4ms
memory: 15704kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 2ms
memory: 15708kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 2ms
memory: 15676kb
input:
10 0 ++?+??++??
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 2ms
memory: 15680kb
input:
12 100 +?+++??+??++
output:
19
result:
ok single line: '19'
Test #6:
score: 0
Accepted
time: 5ms
memory: 15692kb
input:
1 1 ?
output:
0
result:
ok single line: '0'
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 15704kb
input:
9 7 ++++++++?
output:
8
result:
wrong answer 1st lines differ - expected: '7', found: '8'