답안 #248361

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248361 2020-07-12T09:51:17 Z Vimmer Sirni (COCI17_sirni) C++14
42 / 140
2264 ms 174348 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 <int> > mp[N];

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[j][valer(i, j)].pb(i);
                  }

          }

        for (int i = 0; i < n; i++)
        {
            int a = fnd(i);

            for (auto it : mp[i])
              for (auto itr : it.S)
                {
                    int b = fnd(itr);

                    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 601 ms 51776 KB Output is correct
2 Correct 480 ms 51812 KB Output is correct
3 Correct 719 ms 51812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 535 ms 51812 KB Output is correct
2 Correct 275 ms 47592 KB Output is correct
3 Correct 716 ms 51812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 705 ms 51812 KB Output is correct
2 Correct 544 ms 51820 KB Output is correct
3 Correct 710 ms 51812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1669 ms 100212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 143 ms 57976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2264 ms 126232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 334 ms 58516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2094 ms 174348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2145 ms 172756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 221 ms 65144 KB Output isn't correct
2 Halted 0 ms 0 KB -