QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#574100#9313. Make MaxRetr00RE 0ms3588kbC++201.3kb2024-09-18 20:47:342024-09-18 20:47:34

Judging History

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

  • [2024-09-18 20:47:34]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3588kb
  • [2024-09-18 20:47:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 21, INF = 0x3f3f3f3f;
ll ans;

struct node
{
    ll idx, val, fa, mul, son[2];
    friend bool operator<(node a, node b) { return a.idx < b.idx; }
    void init(ll _idx, ll _val, ll _fa, ll _mul)
    {
        idx = _idx, val = _val, fa = _fa, mul = _mul, son[0] = son[1] = 0;
    }
} tree[N];

ll build(ll n)
{
    for (int i = 1; i <= n; i++)
    {
        ll k = i - 1;
        while (tree[k].val < tree[i].val)
            k = tree[k].fa;
        tree[i].son[0] = tree[k].son[1];
        tree[k].son[1] = i;
        tree[i].fa = k;
        tree[tree[i].son[0]].fa = i;
    }
    return tree[0].son[1];
}

void dfs(ll x, ll d)
{
    if (!x)
        return;
    ans += d * tree[x].mul;
    dfs(tree[x].son[0], d + 1);
    dfs(tree[x].son[1], d + 1);
    return;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        ll n, h, rt;
        ans = 0;
        cin >> n;
        tree[0].init(0, INF, 0, 0);
        for (int i = 1; i <= n; i++)
        {
            cin >> h;
            if (h == tree[i - 1].val)
                tree[i - 1].mul++, i--, n--;
            else
                tree[i].init(i, h, 0, 1);
        }
        rt = build(n);
        dfs(rt, 0);
        cout << ans << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
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
0
3
3

result:

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

Test #2:

score: -100
Runtime Error

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:


result: