Submission #1092867

# Submission time Handle Problem Language Result Execution time Memory
1092867 2024-09-25T09:22:34 Z vjudge1 Sirni (COCI17_sirni) C++14
14 / 140
900 ms 190788 KB
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
struct pi
{
    int w, u, v;
};
bool cmp(pi a, pi b)
{
    if (a.w <= b.w)
        return true;
    return false;
}
int par[100005];
int fid(int x)
{
    if (par[x] == x)
        return x;
    return par[x] = fid(par[x]);
}
void make_pair(int x, int y)
{
    int u = fid(x);
    int v = fid(y);
    if (u == v)
        return;
    if (u < v)
        swap(u, v);
    par[u] = v;
}
vector<pi> v;
int mp[10000007], nxt[10000007];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        par[i] = i;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++)
        mp[a[i]] = i;
    int k = -1;
    for (int i = 10000002; i >= 0; i--)
    {
        if (mp[i] != 0)
            k = mp[i];
        nxt[i] = k;
    }
    for (int i = 1; i <= n; i++)
    {
        k = 1;
        while (a[i] * k <= 10000000)
        {
            int x = nxt[a[i] * k];
            if (x == i)
            {
                x++;
                if (x == n + 1)
                    x = -1;
            }
            if (x == -1)
                break;
            v.push_back({a[x] % a[i], i, x});
            k = a[x] / a[i] + 1;
            // cout << i << " " << x << " " << a[x] % a[i] << '\n';
        }
    }
    sort(v.begin(), v.end(), cmp);
    int ans = 0;
    for (auto x : v)
    {
        if (fid(x.u) == fid(x.v))
            continue;
        make_pair(x.u, x.v);
        ans += x.w;
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 43356 KB Output is correct
2 Runtime error 127 ms 89872 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 88 ms 80724 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 43508 KB Output is correct
2 Correct 23 ms 42020 KB Output is correct
3 Correct 25 ms 43536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 373 ms 115104 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 88 ms 91944 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 900 ms 153976 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 69 ms 91616 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 517 ms 190788 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 472 ms 190508 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 139 ms 154296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -