Submission #945862


Source Code Expand

#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp> // Общий файл. 
#include <ext/pb_ds/tree_policy.hpp> // Содержит класс tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = a; i < n; ++i)
#define ford(i, a, n) for (int i = n - 1; i >= a; --i)
#define fore(i, a, n) for (int i = a; i <= n; ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)\
    for (auto i : a) cerr << i << ' ';\
    cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

ll Pow(ll a, ll b) {
    ll x = 1, y = a;
    while (b) {
        if (b % 2) {
            x = x * y % MOD;
        }
        y = y * y % MOD;
        b /= 2;
    }
    return x;
}

ll Inv(ll x) {
    return Pow(x, MOD - 2);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<ll> fact(n + 1);
    fact[0] = 1;
    fore(i, 1, n) fact[i] = fact[i - 1] * i % MOD;
    vector<int> p(n);
    ordered_set frees;
    forn(i, 0, n) frees.insert(i);
    forn(i, 0, n) {
        cin >> p[i];
        --p[i];
        if (p[i] != -1)
            frees.erase(p[i]);
    }
    ordered_set ps;
    int free = 0;
    forn(i, 0, n) if (p[i] == -1) ++free;
    ll ans = 0;
    ll freeAfter = 0;
    ll avg = 0;
    ford(i, 0, n) {
        if (p[i] == -1) {
            ll cnt = avg * Inv(free) % MOD;
            cnt += freeAfter * Inv(2);
            cnt %= MOD;
            ans = (ans + cnt * fact[free] % MOD * fact[n - 1 - i]) % MOD;
            ++freeAfter;
        } else {
            ll cnt = ps.order_of_key(p[i]);
            ps.insert(p[i]);
            ll add = freeAfter * frees.order_of_key(p[i]) % MOD * Inv(free) % MOD;
            ll rez = (cnt + add) * fact[free] % MOD * fact[n - 1 - i];
            ans = (ans + rez) % MOD;
            avg += free - frees.order_of_key(p[i]);
            if (avg >= MOD) avg -= MOD;
        }
    }
    ans += fact[free];
    ans %= MOD;
    cout << ans << '\n';
}

Submission Info

Submission Time
Task E - Encyclopedia of Permutations
User khadaev
Language C++14 (GCC 5.4.1)
Score 1200
Code Size 2501 Byte
Status AC
Exec Time 1836 ms
Memory 29568 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 500 / 500 700 / 700
Status
AC × 5
AC × 33
AC × 58
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt
Subtask 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt, 2_037.txt, 2_038.txt, 2_039.txt, 2_040.txt, 2_041.txt, 2_042.txt, 2_043.txt, 2_044.txt, 2_045.txt, 2_046.txt, 2_047.txt, 2_048.txt, 2_049.txt, 2_050.txt, 2_051.txt, 2_052.txt, 2_053.txt, 2_054.txt, 2_055.txt, 2_056.txt, 2_057.txt
Case Name Status Exec Time Memory
0_000.txt AC 3 ms 256 KB
0_001.txt AC 3 ms 256 KB
0_002.txt AC 3 ms 256 KB
0_003.txt AC 3 ms 256 KB
0_004.txt AC 3 ms 256 KB
1_005.txt AC 3 ms 256 KB
1_006.txt AC 3 ms 256 KB
1_007.txt AC 3 ms 256 KB
1_008.txt AC 4 ms 384 KB
1_009.txt AC 4 ms 384 KB
1_010.txt AC 5 ms 512 KB
1_011.txt AC 4 ms 384 KB
1_012.txt AC 5 ms 384 KB
1_013.txt AC 3 ms 256 KB
1_014.txt AC 5 ms 384 KB
1_015.txt AC 4 ms 384 KB
1_016.txt AC 5 ms 384 KB
1_017.txt AC 3 ms 256 KB
1_018.txt AC 5 ms 384 KB
1_019.txt AC 4 ms 384 KB
1_020.txt AC 5 ms 384 KB
1_021.txt AC 4 ms 384 KB
1_022.txt AC 5 ms 384 KB
1_023.txt AC 4 ms 384 KB
1_024.txt AC 5 ms 384 KB
1_025.txt AC 3 ms 256 KB
1_026.txt AC 5 ms 384 KB
1_027.txt AC 4 ms 384 KB
1_028.txt AC 4 ms 384 KB
1_029.txt AC 5 ms 512 KB
1_030.txt AC 5 ms 384 KB
1_031.txt AC 5 ms 384 KB
1_032.txt AC 5 ms 384 KB
2_033.txt AC 689 ms 29568 KB
2_034.txt AC 305 ms 14848 KB
2_035.txt AC 836 ms 29568 KB
2_036.txt AC 762 ms 27136 KB
2_037.txt AC 993 ms 29568 KB
2_038.txt AC 845 ms 25984 KB
2_039.txt AC 1152 ms 29568 KB
2_040.txt AC 485 ms 14848 KB
2_041.txt AC 1293 ms 29568 KB
2_042.txt AC 159 ms 5888 KB
2_043.txt AC 1432 ms 29568 KB
2_044.txt AC 852 ms 19456 KB
2_045.txt AC 1599 ms 29568 KB
2_046.txt AC 1495 ms 28288 KB
2_047.txt AC 1712 ms 29568 KB
2_048.txt AC 591 ms 13184 KB
2_049.txt AC 1836 ms 29568 KB
2_050.txt AC 1043 ms 19456 KB
2_051.txt AC 1570 ms 29568 KB
2_052.txt AC 1378 ms 26752 KB
2_053.txt AC 682 ms 29568 KB
2_054.txt AC 1567 ms 29568 KB
2_055.txt AC 1336 ms 29568 KB
2_056.txt AC 1364 ms 29568 KB
2_057.txt AC 1175 ms 29568 KB