답안 #248365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248365 2020-07-12T09:54:15 Z Vimmer Sirni (COCI17_sirni) C++14
140 / 140
3758 ms 769056 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100010
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 6> a6;

//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;



int pr[N], rk[N], kol;

vector <int> a;

void make(int x) {pr[x] = x; rk[x] = 1;}

int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}

int valer(int x, int y) {return min(a[x] % a[y], a[y] % a[x]);}

void link(int a, int b)
{
    if (rk[b] > rk[a]) swap(a, b);

    pr[b] = a;

    rk[a] += rk[b];
}

gp_hash_table<int, vector <pair <int, int> > > mp;

bool cmp(pair <int, int> x, pair <int, int> y){return valer(x.F, x.S) < valer(y.F, y.S);}

int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;

    cin >> n;

    a.resize(n);

    int mx = 0;

    for (int i = 0; i < n; i++) {cin >> a[i]; mx = max(mx, a[i]);}

    sort(a.begin(), a.end());

    a.resize(unique(a.begin(), a.end()) - a.begin());

    n = sz(a);

    ll ans = 0;

    for (int i = 0; i < n; i++) make(i);

    vector <pair <int, int> > gr; gr.clear();

    if (n <= 1000)
    {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                if (i == j) continue;

                gr.pb({i, j});
            }

        sort(gr.begin(), gr.end(), cmp);

        for (auto it : gr)
        {
            int x = fnd(it.F), y = fnd(it.S);

            if (x == y) continue;

            ans += valer(it.F, it.S);

            link(x, y);

            kol++;

            if (kol == n - 1) break;
        }
    }
    else
    {
        for (int i = 0; i < n; i++)
          {
              int j = i + 1;

              for (int u = a[i]; u <= mx && j < n; u += a[i])
                  {
                      while (j < n && a[j] < u) j++;

                      if (j != n) mp[valer(i, j)].pb({j, i});
                  }

          }

        for (auto it : mp)
        {
          for (auto itr : it.S)
            {
                int b = fnd(itr.S), a = fnd(itr.F);

                if (a == b) continue;

                ans += it.F;

                link(a, b);

                kol++;

                if (kol == n - 1) {cout << ans << endl; exit(0);}
            }
        }
    }


    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 574 ms 8688 KB Output is correct
2 Correct 459 ms 8688 KB Output is correct
3 Correct 700 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 517 ms 8688 KB Output is correct
2 Correct 243 ms 4596 KB Output is correct
3 Correct 684 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 673 ms 8688 KB Output is correct
2 Correct 512 ms 8688 KB Output is correct
3 Correct 681 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1226 ms 13324 KB Output is correct
2 Correct 1193 ms 51788 KB Output is correct
3 Correct 902 ms 38544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 3328 KB Output is correct
2 Correct 141 ms 29988 KB Output is correct
3 Correct 867 ms 14788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1299 ms 24188 KB Output is correct
2 Correct 1375 ms 70764 KB Output is correct
3 Correct 1246 ms 30664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 5004 KB Output is correct
2 Correct 1325 ms 71644 KB Output is correct
3 Correct 1004 ms 36948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1371 ms 17348 KB Output is correct
2 Correct 3418 ms 706324 KB Output is correct
3 Correct 1359 ms 21180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1406 ms 25736 KB Output is correct
2 Correct 3758 ms 769056 KB Output is correct
3 Correct 1883 ms 134348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 3156 KB Output is correct
2 Correct 2905 ms 476312 KB Output is correct
3 Correct 1070 ms 35048 KB Output is correct