QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#497655#6433. Klee in Solitary ConfinementUmokML 0ms0kbC++201.8kb2024-07-29 15:34:592024-07-29 15:34:59

Judging History

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

  • [2024-07-29 15:34:59]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-07-29 15:34:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define uint unsigned long long
#define int long long
#define endl '\n'
const int N = 1e7 + 5;
typedef pair<int, int> PII;
#define MAX LONG_LONG_MAX
int ar[N];
int num[N];
int pre[N], back[N];
void solve()
{
    int n, m, ans = 0;
    cin >> n >> m;
    vector<vector<int>> v(2 * N);
    for (int i = 1; i <= n; i++)
        pre[i] = back[i] = 0;

    for (int i = 1; i <= n; i++)
    {
        cin >> ar[i];
        v[ar[i] + N].push_back(i);
        num[ar[i]]++;
        pre[i] = num[ar[i]];
        ans = max(ans, num[ar[i]]);
    }
    memset(num, 0, sizeof num);

    for (int i = n; i; i--)
    {
        num[ar[i]]++;
        back[i] = num[ar[i]];
    }

    for (int i = 1; i <= n; i++)
    {
        auto l = upper_bound(v[ar[i] - m + N].begin(), v[ar[i] - m + N].end(), i);
        auto r = v[ar[i] - m + N].end();
        if (l != r)
        {
            r--;
            auto idxs = upper_bound(v[ar[i] + N].begin(), v[ar[i] + N].end(), *r);
            ans = max(ans, pre[i] + back[*l]);
            if (r != l && idxs != v[ar[i] + N].end() && (*r) < (*idxs))
                ans = max(ans, pre[i] + back[*l] + back[*idxs]);
        }
         l = upper_bound(v[ar[i] + m + N].begin(), v[ar[i] + m + N].end(), i);
        r = v[ar[i] + m + N].end();
        if (l != r)
        {
            r--;
            auto idxs = upper_bound(v[ar[i] + N].begin(), v[ar[i] + N].end(), *r);
            ans = max(ans, pre[i] + back[*l]);
            if (r != l && idxs != v[ar[i] + N].end() && (*r) < (*idxs))
                ans = max(ans, pre[i] + back[*l] + back[*idxs]);
        }
    }

    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

5 2
2 2 4 4 4

output:

5

result: