QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#137338#2356. Partition of QueriesS_Explosion#WA 2ms7984kbC++201.4kb2023-08-10 10:39:322023-08-10 10:39:34

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-10 10:39:34]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7984kb
  • [2023-08-10 10:39:32]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>

template <typename Tp>
inline void read(Tp &x) {
    x = 0;
    bool f = true; char ch = getchar();
    for ( ; ch < '0' || ch > '9'; ch = getchar()) f ^= ch == '-';
    for ( ; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + (ch ^ 48);
    x = f ? x : -x;
}

const int N = 1e6;

long long sum[N + 5], dp[N + 5];
int a[N + 5], q[N + 5], id[N + 5];
char s[N + 5];

long long get(int i, int j) {
    return ((dp[i] - sum[i]) - (dp[j] - sum[j]));
}

int main() {
    int n, m;
    read(n), read(m);
    scanf("%s", s + 1);
    for (int i = 1; i <= n; ++i) {
        a[i] = a[i - 1];
        if (s[i] == '+') ++a[i];
    }
    for (int i = n; i >= 0; --i) {
        sum[i] = sum[i + 1];
        if (s[i] == '?') sum[i] += a[i];
    }
    int l = 1, r = 1, now = 0; q[l] = n + 1;
    for (int i = n; i >= 0; --i) {
        if (s[i] == '+') continue;
        id[i] = ++now;
        while (l < r && get(q[l + 1], q[l]) < 1LL * (-a[i]) * (id[q[l + 1]] - id[q[l]])) ++l;
        while (l < r && get(q[r], q[r - 1]) * (id[i] - id[q[r]]) > get(i, q[r]) * (id[q[r]] - id[q[r - 1]])) --r;
        q[++r] = i;
        dp[i] = dp[q[l]] + sum[i] - sum[q[l]] - 1LL * a[i] * (id[i] - id[q[l]]);
        if (i) dp[i] += m;
        // printf("%d %lld\n", q[l], dp[i]);
    }
    printf("%lld\n", dp[0]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5704kb

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 5660kb

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 2ms
memory: 7980kb

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5696kb

input:

10 0
++?+??++??

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 2ms
memory: 7840kb

input:

12 100
+?+++??+??++

output:

19

result:

ok single line: '19'

Test #6:

score: 0
Accepted
time: 2ms
memory: 7760kb

input:

1 1
?

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 2ms
memory: 7756kb

input:

9 7
++++++++?

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 2ms
memory: 7984kb

input:

9 8
++++++++?

output:

8

result:

ok single line: '8'

Test #9:

score: 0
Accepted
time: 0ms
memory: 7756kb

input:

10 15
++++++++??

output:

15

result:

ok single line: '15'

Test #10:

score: 0
Accepted
time: 1ms
memory: 5772kb

input:

5 3
+?+?+

output:

3

result:

ok single line: '3'

Test #11:

score: -100
Wrong Answer
time: 1ms
memory: 5804kb

input:

10 5
+?+?+??+??

output:

16

result:

wrong answer 1st lines differ - expected: '10', found: '16'