QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#573987#9313. Make MaxAnndy_bonusWA 902ms7176kbC++171.5kb2024-09-18 20:30:402024-09-18 20:30:40

Judging History

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

  • [2024-09-18 20:30:40]
  • 评测
  • 测评结果:WA
  • 用时:902ms
  • 内存:7176kb
  • [2024-09-18 20:30:40]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;
constexpr int N = 2e5 + 10;

int stk[N], top = -1;
int l[N], r[N];

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n + 2);
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
    }
    top = -1;
    stk[++top] = 0;
    for (int i = 1; i <= n; i++) {
        while (top != 0 && a[stk[top]] <= a[i]) {
            top--;
        }
        l[i] = stk[top];
        stk[++top] = i;
    }
    top = -1;
    stk[++top] = n + 1;
    for (int i = n; i >= 1; i--) {
        while (top != 0 && a[stk[top]] <= a[i]) {
            top--;
        }
        r[i] = stk[top];
        stk[++top] = i;
    }
    std::vector<int> p(n + 1), dp(n + 2);
    std::iota(p.begin() + 1, p.end(), 1);
    dp[0] = dp[n + 1] = -1;
    std::sort(p.begin() + 1, p.end(),
              [&](int x, int y) { return a[p[x]] > a[p[y]]; });
    LL ans = 0;
    for (int i = 1; i <= n; i++) {

        if (l[p[i]] == 0 || r[p[i]] == n + 1 || a[l[p[i]]] == a[r[p[i]]]) {
            dp[p[i]] = std::max(dp[l[p[i]]], dp[r[p[i]]]) + 1;
        } else {
            dp[p[i]] = std::max(dp[l[p[i]]], dp[r[p[i]]]) + 2;
        }
        ans += 1LL * dp[p[i]];
    }
    std::cout << ans << '\n';
}

int main() {
    // freopen("ans.in", "r", stdin);
    // freopen("ans.out", "w", stdout);
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int T;
    std::cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Wrong Answer
time: 902ms
memory: 7176kb

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:

1803988
1252549

result:

wrong answer 1st numbers differ - expected: '4084978', found: '1803988'