QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#137327#2356. Partition of QueriesS_Explosion#WA 2ms7948kbC++201.4kb2023-08-10 10:29:362023-08-10 10:29:36

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:29:36]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7948kb
  • [2023-08-10 10:29:36]
  • 提交

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];

double slope(int i, int j) {
    return 1.0 * ((dp[i] - sum[i]) - (dp[j] - sum[j])) / (id[i] - id[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 && slope(q[l + 1], q[l]) < -a[i]) ++l;
        while (l < r && slope(q[r], q[r - 1]) > slope(i, q[r])) --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;
}

詳細信息

Test #1:

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

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

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

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

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

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

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

input:

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

output:

0

result:

ok single line: '0'

Test #5:

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

input:

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

output:

19

result:

ok single line: '19'

Test #6:

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

input:

1 1
?

output:

0

result:

ok single line: '0'

Test #7:

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

input:

9 7
++++++++?

output:

7

result:

ok single line: '7'

Test #8:

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

input:

9 8
++++++++?

output:

8

result:

ok single line: '8'

Test #9:

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

input:

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

output:

15

result:

ok single line: '15'

Test #10:

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

input:

5 3
+?+?+

output:

3

result:

ok single line: '3'

Test #11:

score: -100
Wrong Answer
time: 2ms
memory: 7948kb

input:

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

output:

16

result:

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