結果

問題 No.2289 順列ソート
ユーザー ococonomy1ococonomy1
提出日時 2023-05-05 22:42:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 6,408 bytes
コンパイル時間 2,014 ms
コンパイル使用メモリ 127,964 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 05:34:00
合計ジャッジ時間 3,316 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <tuple>
#include <queue>
#include <bitset>
using namespace std;
using lg = long long;
#define TEST clog << "TEST" << endl
#define IINF 2147483647
#define LLINF 9223372036854775807LL
#define AMARI 998244353
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'

//RMQとRSQをまとめたセグ木(遅延ではない)
//最小値の取得と区間和の取得ができる 関数名さえ見てればrmqとかrsqとかあんま考えなくても行けるはず
//最小値の取得をする場合について、syokica()内の初期値の設定だけIINFになっているところを、その型で取り得る最大値に書き換えた方が安全
template<typename T>
class ococo_segtree {
public:
    int n;
    T tmax;
    //range sum query
    vector<T> rsq;
    //range minimum query
    vector<T> rmq;

    ococo_segtree(int N = 0, T t = IINF) {
        syokica(N, t);
    }

    //配列の初期化 O(N)
    void syokica(int a, T t) {
        n = 1;
        tmax = t;
        while (n < a)n *= 2;
        rsq.resize(2 * n - 1);
        rmq.resize(2 * n - 1);
        for (int i = 0; i < 2 * n - 1; i++) {
            rsq[i] = 0;
            //型Tのmaxの値を入れたい
            rmq[i] = tmax;
        }
    }

    //a[i]をxにする O(logN)
    void update_num(int i, T x) {
        i += (n - 1);
        rmq[i] = x;
        T dif = x - rsq[i];
        rsq[i] += dif;
        while (i) {
            i = (i - 1) / 2;
            rmq[i] = min(rmq[i * 2 + 1], rmq[i * 2 + 2]);
            rsq[i] += dif;
        }
    }

    //a[i]にxを加える O(logN)
    void add_num(int i, T x) {
        i += (n - 1);
        rsq[i] += x;
        rmq[i] = min(rmq[i], x);
        while (i) {
            i = (i - 1) / 2;
            rsq[i] += x;
            rmq[i] = min(rmq[i * 2 + 1], rmq[i * 2 + 2]);
        }
    }

    T get_sum2(int a, int b, int k, int l, int r) {
        //cout << k << endl;
        if (a <= l && r <= b)return rsq[k];
        else if (r <= a || b <= l)return 0;
        else {
            T temp = (get_sum2(a, b, k * 2 + 1, l, (l + r) / 2) + get_sum2(a, b, k * 2 + 2, (l + r) / 2, r));
            return temp;
        }
    }
    //[l,r)の和の取得 O(logN)
    T get_sum(int l, int r) {
        return get_sum2(l, r, 0, 0, n);
    }

    T get_minimum2(int a, int b, int k, int l, int r) {
        if (a <= l && r <= b)return rmq[k];
        else if (r <= a || b <= l)return tmax;
        else return min(get_minimum2(a, b, k * 2 + 1, l, (l + r) / 2), get_minimum2(a, b, k * 2 + 2, (l + r) / 2, r));
    }
    //[l,r)の最小値の取得 O(logN)
    T get_minimum(int l, int r) {
        return get_minimum2(l, r, 0, 0, n);
    }
};


class ococo_unionfind {
    //できること
    //点の挿入
    //その点の根を求める関数
    //辺の挿入
    //連結判定
    //島が何個あるかの出力
    //それぞれの島について、何個の点があるかの出力
public:
    ococo_unionfind(int n = 0) {
        for (int i = 0; i < n; i++)vinsert();
    }
    int simakosuu = 0;
    //g[i] = {その点の一個上の点,その点のrank}
    //その点のrank:その点の下に何個点があるか(上に何個あるかに変えた方がいいかも?)
    vector<pair<int, int>> g;

    //rs[i] = その点が含まれている連結成分に何個の点があるか
    //その連結成分の根について聞かないと返さない
    vector<int> rs;
    //点の挿入 O(1)
    void vinsert(void) {
        g.emplace_back(g.size(), 1);
        simakosuu++;
        rs.push_back(1);
    }
    //ある点の根を求める関数 O(α(N))
    int ne(int a) {
        if (g[a].first == a)return a;
        else {
            return g[a].first = ne(g[a].first);
        }
    }
    //辺の挿入 O(logN)
    void einsert(int a, int b) {
        if (a != b) {
            int a1 = ne(a), a2 = ne(b);
            if (a1 != a2) {
                simakosuu--;
                int rs12sum = rs[a1] + rs[a2];
                rs[a1] = rs12sum;
                rs[a2] = rs12sum;
                if (g[a1].second < g[a2].second) {
                    g[a1].first = a2;
                    g[a2].second = max(g[a1].second + 1, g[a2].second);
                }
                else {
                    g[a2].first = a1;
                    g[a1].second = max(g[a2].second + 1, g[a1].second);
                }
            }
        }
    }
    //2つのノードが繋がっているか判定する関数 O(α(N))
    bool renketucheck(int a, int b) {
        if (ne(a) == ne(b))return true;
        else return false;
    }
    //何個の島に分かれているか出力する関数 O(1)
    int islandnum(void) {
        return simakosuu;
    }
    //ある点について、その点が含まれている連結成分が何個の点を持つか返す関数 O(α(N))
    int islandsize(int a) {
        return rs[ne(a)];
    }
};


#define MULTI_TEST_CASE false
void solve(void) {
    int n;
    cin >> n;
    vector<int> a(n);
    ococo_unionfind ouf(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        ouf.einsert(a[i], i);
    }
    vector<vector<int>> v(n);
    for (int i = 0; i < n; i++) {
        v[ouf.ne(i)].push_back(a[i]);
    }
    lg ans = 0;
    for (int i = 0; i < n; i++) {
        if (v[i].size() <= 1)continue;
        //for (int j = 0; j < v[i].size(); j++)clog << v[i][j] << ' ';
//        clog << el;
        set<int> s;
        for (int j = 0; j < v[i].size(); j++)s.insert(v[i][j]);
        map<int, int> mp;
        int cnt = 0;
        for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
            mp[*it] = cnt;
            cnt++;
        }
        //ans += max(mp[v[i][0]], (int)v[i].size() - mp[v[i][0]]);
        ans += v[i].size() - 1;
    }
    cout << ans << el;
    return;
}

void calc(void) {
    return;
}

int main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    if (MULTI_TEST_CASE)cin >> t;
    while (t--) {
        solve();
    }
    calc();
    return 0;
}
0