QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#22483 | #2356. Partition of Queries | li0201# | WA | 5ms | 17760kb | C++20 | 1.8kb | 2022-03-09 19:04:16 | 2022-04-30 01:11:44 |
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 && g[q[hd + 1]] - g[q[hd]] < c[i] * (w[q[hd + 1]] - w[q[hd]])) ++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 && (g[i] - g[q[tl - 1]]) * (w[q[tl]] - w[q[tl - 1]]) < (g[q[tl]] - g[q[tl - 1]]) * (w[i] - w[q[tl - 1]])) --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: 1ms
memory: 17676kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 5ms
memory: 17740kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 5ms
memory: 17760kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: -100
Wrong Answer
time: 3ms
memory: 17684kb
input:
10 0 ++?+??++??
output:
8
result:
wrong answer 1st lines differ - expected: '0', found: '8'