답안 #1106868

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106868 2024-10-31T08:17:17 Z vjudge1 Sirni (COCI17_sirni) C++14
84 / 140
868 ms 786432 KB
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
using ll = long long;
const int nx=1e5+5;
const int N=2e7+5;
int n, a[nx], last[N], par[nx];
ll ans=0;
vector<int> rar;
vector<pair<int, ii>> ed;
int find(int u)
{
    if(!par[u])
        return u;
    return par[u]=find(par[u]);
}
bool join(int u, int v)
{
    u=find(u);
    v=find(v);
    if(u==v)
        return 0;
    par[v]=u;
    return 1;
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>a[i], rar.emplace_back(a[i]);
    sort(rar.begin(), rar.end());
    rar.erase(unique(rar.begin(), rar.end()), rar.end());
    n=rar.size();
    for(int i = 1; i <= n; i++) 
    {
        a[i]=rar[i-1];
        last[a[i]]=i;
    }
    for(int i = N-2; i >= 1; i--)
    {
        if(last[i])
            continue;
        if(last[i+1])
            last[i]=last[i+1];
    }
    for(int i = 1; i <= n; i++)
    {
        if(a[i]==0)
        assert(0);
        if(last[a[i]+1])
            ed.push_back({a[last[a[i]+1]]%a[i], {i, last[a[i]+1]}});
        for(int j = 2*a[i]; j < N; j+=a[i])
            if(last[j])
                ed.push_back({a[last[j]]%a[i], {i, last[j]}});
    }
    sort(ed.begin(), ed.end());
    for(auto it:ed)
        if(join(it.se.fi, it.se.se))
            ans+=it.fi;
    cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 41552 KB Output is correct
2 Correct 218 ms 92920 KB Output is correct
3 Correct 28 ms 42004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 2636 KB Output is correct
2 Runtime error 649 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 41552 KB Output is correct
2 Correct 25 ms 41504 KB Output is correct
3 Correct 20 ms 41808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 34376 KB Output is correct
2 Correct 614 ms 59060 KB Output is correct
3 Correct 285 ms 59168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 10484 KB Output is correct
2 Correct 330 ms 59152 KB Output is correct
3 Correct 218 ms 32296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 375 ms 59172 KB Output is correct
2 Correct 851 ms 108316 KB Output is correct
3 Correct 255 ms 34612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 10128 KB Output is correct
2 Correct 868 ms 108308 KB Output is correct
3 Correct 253 ms 34612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 69328 KB Output is correct
2 Runtime error 714 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 69428 KB Output is correct
2 Runtime error 711 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 45308 KB Output is correct
2 Runtime error 734 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -