제출 #1020369

#제출 시각아이디문제언어결과실행 시간메모리
1020369vjudge1무제 (POI11_rot)C++17
100 / 100
388 ms65536 KiB
#include <iostream>
#include <vector>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int c = 1;
ordered_set s[400005];
long long int dfs(int x) {
    int w;
    cin >> w;
    if (w != 0) {
        s[x].insert(w);
        return 0;
    }
    long long int res = 0, d = 0, h = 1;
    {
        int w = ++c;
        d += dfs(w);
        h *= s[w].size();
        int flag = 0;
        if (s[w].size() > s[x].size()) {
            s[x].swap(s[w]);
            flag = 1;
        }
        for (int v : s[w]) {
            if (flag == 1) res += s[x].order_of_key(v);
            else res += s[x].size() - s[x].order_of_key(v);
        }
        for (int v : s[w]) {
            s[x].insert(v);
        }
        s[w].clear();
    }
    {
        int w = ++c;
        d += dfs(w);
        h *= s[w].size();
        int flag = 0;
        if (s[w].size() > s[x].size()) {
            s[x].swap(s[w]);
            flag = 1;
        }
        for (int v : s[w]) {
            if (flag == 1) res += s[x].order_of_key(v);
            else res += s[x].size() - s[x].order_of_key(v);
        }
        for (int v : s[w]) {
            s[x].insert(v);
        }
        s[w].clear();
    }
    return min(res, h - res) + d;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    cout << dfs(1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...