QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#137435 | #2356. Partition of Queries | Team_name# | WA | 2ms | 7764kb | C++20 | 1.9kb | 2023-08-10 12:39:32 | 2023-08-10 12:39:35 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define _debugVar(x) { cerr << "* "#x" = " << x << endl; }
#define _debugHere { fprintf(stderr, "* Passing [%s] in Line %d\n", __FUNCTION__, __LINE__); }
using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e6+5;
int n;
LL m;
char str[N];
LL cnta[N], cntq[N], sum[N];
LL f[N];
double slope(int i, int j)
{
static const auto X = [&](int j) -> double {
return (double)cnta[j];
};
static const auto Y = [&](int j) -> double {
return (double)cnta[j]*cntq[j]-sum[j]+f[j];
};
// fprintf(stderr, "* X(%d) = %.6lf\n", i, X(i));
// fprintf(stderr, "* Y(%d) = %.6lf\n", i, Y(i));
// fprintf(stderr, "* X(%d) = %.6lf\n", j, X(j));
// fprintf(stderr, "* Y(%d) = %.6lf\n", j, Y(j));
double res = (Y(j)-Y(i))/(X(j)-X(i));
// fprintf(stderr, "slope(%d, %d) = %.6lf\n", i, j, res);
return res;
}
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d%lld", &n, &m);
scanf(" %s", str+1);
for(int i = 1; i <= n; i++) {
cnta[i] = cnta[i-1]+(str[i] == '+');
cntq[i] = cntq[i-1]+(str[i] == '?');
sum[i] = sum[i-1]+(str[i] == '?')*cnta[i];
}
static int q[N];
int hh = 1, tt = 0;
f[0] = -m;
q[++tt] = 0;
for(int i = 1; i <= n; i++) {
//_debugVar(slope(q[hh], q[hh+1]));
// int j = hh;
// while(j < tt && slope(q[j], q[j+1]) >= cntq[i]) j++;
// while(hh < tt && slope(q[tt-1], q[tt]) <= cntq[i]) tt--;
while(hh < tt && slope(q[hh], q[hh+1]) <= cntq[i]) hh++;
int j = q[hh];
f[i] = f[j]+(sum[i]-sum[j])-(cntq[i]-cntq[j])*cnta[j]+m;
// fprintf(stderr, "f[%d] = %lld from %d\n", i, f[i], j);
while(hh < tt && slope(q[tt-1], q[tt]) >= slope(q[tt-1], i)) tt--;
q[++tt] = i;
}
// for(int i = 1; i <= n; i++) {
// for(int j = 0; j <= i-1; j++) {
// f[i] = min(f[i], f[j]+(sum[i]-sum[j])-(cntq[i]-cntq[j])*cnta[j]+m);
// }
// }
printf("%lld\n", f[n]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7764kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5896kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 2ms
memory: 7700kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 7744kb
input:
10 0 ++?+??++??
output:
8
result:
wrong answer 1st lines differ - expected: '0', found: '8'