답안 #248356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248356 2020-07-12T09:40:25 Z Vimmer Sirni (COCI17_sirni) C++14
98 / 140
5000 ms 527024 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];
}

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});
            }
    }
    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) 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;
    }

    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 564 ms 8688 KB Output is correct
2 Correct 448 ms 8688 KB Output is correct
3 Correct 686 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 507 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 678 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1754 ms 18096 KB Output is correct
2 Correct 3478 ms 34372 KB Output is correct
3 Correct 2052 ms 34372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 2676 KB Output is correct
2 Correct 1386 ms 33736 KB Output is correct
3 Correct 1415 ms 17880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2534 ms 34488 KB Output is correct
2 Correct 4712 ms 67228 KB Output is correct
3 Correct 2182 ms 18064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 339 ms 4972 KB Output is correct
2 Correct 4722 ms 67104 KB Output is correct
3 Correct 2062 ms 17892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2078 ms 18012 KB Output is correct
2 Execution timed out 5098 ms 526956 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2343 ms 18016 KB Output is correct
2 Execution timed out 5059 ms 527024 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 149 ms 2804 KB Output is correct
2 Execution timed out 5109 ms 526800 KB Time limit exceeded
3 Halted 0 ms 0 KB -