QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#220564#6433. Klee in Solitary ConfinementPHarr#WA 1ms3540kbC++201.8kb2023-10-20 15:31:352023-10-20 15:31:36

Judging History

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

  • [2023-10-20 15:31:36]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3540kb
  • [2023-10-20 15:31:35]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;


int main() {
    int n, k;
    cin >> n >> k;
    vi a(n + 1);
    map<int, vi> cnt;
    for (int i = 1; i <= n; i++)
        cin >> a[i], cnt[a[i]].push_back(i);

    auto lst = [&cnt](int x, int i) {
//        cout << x << " " << i << " : " ;
//        for( auto j : cnt[x] ) cout << j << " ";
//        cout << "\n";

        if (cnt[x].empty()) return 0;
        auto it = lower_bound(cnt[x].begin(), cnt[x].end(), i);
        if( it == cnt[x].end() ) return cnt[x].back();
        if (*it < i) return *it;
        if (it == cnt[x].begin()) return 0;
        return *prev(it);
    };

    vi pre(n + 1);
    for (int i = 1; i <= n; i++)
        pre[i] = pre[lst(a[i], i)] + 1;

    vi suf(n + 1);
    for (int i = n, j; i >= 1; i--) {
        suf[i]++, j = lst(a[i], i);
        if (j != 0) suf[j] += suf[i];
    }
//    for (int i = 1; i <= n; i++)
//        cout << pre[i] << " ";
//    cout << "\n";
//    for (int i = 1; i <= n; i++)
//        cout << suf[i] << " ";
//    cout << "\n";

    vi f(n + 1);
    for (int i = 1, j; i <= n; i++) {
        f[i] = pre[i], j = lst(a[i] - k, i);
//        cout << " " << i << " " << a[i] << " " << a[i] - k << " " << j << "\n";
        if (j != 0) f[i] -= pre[j];
    }
//    for (int i = 1; i <= n; i++)
//        cout << f[i] << " ";
//    cout << "\n";


    vi g(n + 1);
    for (int i = 1; i <= n; i++)
        g[i] = max(f[i], g[lst(a[i], i)]);
    for (int i = n, j; i >= 1; i--) {
        j = lst(a[i] + k, i);
        g[i] = pre[i];
        if (j > 0) g[i] += g[j];
    }
    int res = 0;
    for (int i = 1, j, t; i <= n; i++) {
        j = lst(a[i] - k, i), t = suf[i];
        if (j > 1) t += g[j];
        res = max(res, t);
    }
    cout << res << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3416kb

input:

5 2
2 2 4 4 4

output:

5

result:

ok 1 number(s): "5"

Test #2:

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

input:

7 1
3 2 3 2 2 2 3

output:

6

result:

ok 1 number(s): "6"

Test #3:

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

input:

7 1
2 3 2 3 2 3 3

output:

5

result:

ok 1 number(s): "5"

Test #4:

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

input:

9 -100
-1 -2 1 2 -1 -2 1 -2 1

output:

3

result:

ok 1 number(s): "3"

Test #5:

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

input:

200 121649
0 527189 -1000000 -306471 -998939 527189 -1000000 -1000000 0 527189 0 527189 0 527189 -306471 -998939 -306471 -306471 -306471 0 0 527189 527189 1000000 527189 -1000000 1000000 648838 -1000000 -998939 -998939 -998939 0 1000000 -1000000 -998939 527189 1000000 648838 -1000000 1000000 648838 ...

output:

37

result:

ok 1 number(s): "37"

Test #6:

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

input:

200 -454379
-385892 454379 -1000000 373644 -665078 -1000000 -1000000 454379 0 1000000 373644 -1000000 1000000 -385892 -1000000 373644 0 -665078 0 -665078 -1000000 -665078 -385892 -665078 -385892 454379 -665078 -385892 -1000000 454379 1000000 -385892 373644 454379 -1000000 -385892 -1000000 -385892 -1...

output:

40

result:

ok 1 number(s): "40"

Test #7:

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

input:

200 0
451272 -1000000 677452 677452 0 18908 451272 677452 -233144 677452 451272 18908 -1000000 18908 -1000000 0 451272 0 -233144 677452 1000000 451272 1000000 18908 -1000000 0 -233144 451272 1000000 18908 677452 0 677452 0 677452 1000000 -233144 18908 451272 -1000000 -233144 18908 1000000 0 0 -23314...

output:

36

result:

wrong answer 1st numbers differ - expected: '35', found: '36'