답안 #248355

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248355 2020-07-12T09:39:27 Z Vimmer Sirni (COCI17_sirni) C++14
98 / 140
5000 ms 527152 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 562 ms 8688 KB Output is correct
2 Correct 448 ms 8688 KB Output is correct
3 Correct 684 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 505 ms 8688 KB Output is correct
2 Correct 246 ms 4596 KB Output is correct
3 Correct 688 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 681 ms 8688 KB Output is correct
2 Correct 517 ms 8688 KB Output is correct
3 Correct 681 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1601 ms 18264 KB Output is correct
2 Correct 3319 ms 34616 KB Output is correct
3 Correct 1914 ms 34304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 2676 KB Output is correct
2 Correct 1347 ms 33732 KB Output is correct
3 Correct 1302 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2317 ms 34500 KB Output is correct
2 Correct 4462 ms 67248 KB Output is correct
3 Correct 2037 ms 18020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 313 ms 4964 KB Output is correct
2 Correct 4495 ms 67160 KB Output is correct
3 Correct 1941 ms 17892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1899 ms 18136 KB Output is correct
2 Execution timed out 5079 ms 527152 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2223 ms 18020 KB Output is correct
2 Execution timed out 5089 ms 526932 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 2804 KB Output is correct
2 Execution timed out 5049 ms 527000 KB Time limit exceeded
3 Halted 0 ms 0 KB -