QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#592847#9313. Make MaxDUSK777WA 0ms3588kbC++141.4kb2024-09-27 08:52:002024-09-27 08:52:01

Judging History

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

  • [2024-09-27 08:52:01]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3588kb
  • [2024-09-27 08:52:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>

void solu() {
    ll n;
    cin >> n;
    vector<pll> a(n); // first为index,second为value
    vector<ll> lindex(n, -1);
    vector<ll> rindex(n, -1);

    for (int i = 0; i < n; ++i) {
        cin >> a[i].second;
        a[i].first = i;
    }

    stack<pll> s;
    
    // 计算右边界
    for (ll i = 0; i < n; ++i) {
        while (!s.empty() && a[i].second > s.top().second) {
            rindex[s.top().first] = a[i].first;
            s.pop();
        }
        s.push(a[i]);
    }
    while (!s.empty()) {
        rindex[s.top().first] = -1;
        s.pop();
    }

    // 计算左边界
    for (ll i = n - 1; i >= 0; --i) {
        while (!s.empty() && a[i].second > s.top().second) {
            lindex[s.top().first] = a[i].first;
            s.pop();
        }
        s.push(a[i]);
    }
    while (!s.empty()) {
        lindex[s.top().first] = -1;
        s.pop();
    }

    ll ans = 0;
    for (ll i = 0; i < n; ++i) {
        ll left_dist = (lindex[i] == -1) ? i : i - lindex[i] - 1;
        ll right_dist = (rindex[i] == -1) ? n - i - 1 : rindex[i] - i - 1;
        ans += left_dist + right_dist;
    }

    cout << ans << endl;
}

int main() {
    ll q;
    cin >> q;
    while (q--) {
        solu();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3588kb

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
2
30
3

result:

wrong answer 2nd numbers differ - expected: '0', found: '2'