QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#807457#8050. Random PermutationPhoenixRebirthWA 1353ms5972kbC++231.7kb2024-12-10 00:06:372024-12-10 00:06:38

Judging History

This is the latest submission verdict.

  • [2024-12-10 00:06:38]
  • Judged
  • Verdict: WA
  • Time: 1353ms
  • Memory: 5972kb
  • [2024-12-10 00:06:37]
  • Submitted

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int N = 300005;
const int MAGIC = 125;

int p[N], n;

int sqr(int x) { return x * x; }

int pool[N << 1], *buc = pool + N;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
    }
    
    // n = 3e5;
    // // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    // mt19937 rng(114514);
    // for (int i = 1; i <= n; i++) p[i] = i;
    // shuffle(p + 1, p + n + 1, rng);

    ll ans = 0, turns = 0;
    for (int i = 1; i <= n; i++) {
        int diff = max(1, abs(n / 2 - p[i]));
        int delta = n / diff + MAGIC;
        int cur = 0, *ptr = buc, tag = 1;
        (*ptr)++;

        for (int j = i - 1; j >= 1; j--) {
            ++turns;
            if (p[j] < p[i]) ptr--;
            else ptr++;
            (*ptr)++;
            if (abs(buc - ptr) > delta) {
                tag = j;
                break;
            }
        }
        cur = 0, ptr = buc;
        ll res = *buc + *(buc + 1);
        for (int j = i + 1; j <= n; j++) {
            ++turns;
            if (p[j] < p[i]) ptr++;
            else ptr--;
            res += *ptr + *(ptr + 1);
            if (abs(buc - ptr) > delta) break;
        }
        ans += res * p[i];

        cur = 0, ptr = buc;
        (*ptr)--;
        for (int j = i - 1; j >= tag; j--) {
            if (p[j] < p[i]) ptr--;
            else ptr++;
            (*ptr)--;
        }
    }
    cerr << "turns = " << turns << '\n';
    printf("%lld\n", ans);
    return 0;
}

/*
mt19937 rng(114514)
3e5: 6741152540127257
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 4 2 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: -100
Wrong Answer
time: 1353ms
memory: 5964kb

input:

100000
56449 21738 74917 44834 36187 96576 37204 28451 3444 13029 66039 8955 51445 30706 27229 37159 66052 16691 70389 29935 44984 3648 75082 73600 76621 28345 5298 37940 49412 85260 92029 18185 84398 10233 79227 98312 96649 30680 65206 38879 75397 26951 11294 58085 37297 97167 59252 44104 4058 3796...

output:

250201078938518

result:

wrong answer 1st numbers differ - expected: '250202478701074', found: '250201078938518'