답안 #990749

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
990749 2024-05-31T07:13:08 Z KawakiMeido Sirni (COCI17_sirni) C++17
98 / 140
679 ms 786584 KB
/*Author: KawakiMeido*/
#include <bits/stdc++.h>
#define pb push_back
#define endl "\n"
#define int long long
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second

using namespace std;

/*Constants*/
const int N = 1e5+50;
const int M = 1e7+50;
const int INF = 1e9+7;
const long long LLINF = 1e18+3;

/*TestCases*/
int test=1;
void solve();
void TestCases(bool v){
    if (v) cin >> test;
    while(test--) solve();
}

/*Global Variables*/
int n;
vector<int> a;
vector<piii> edge;
int vis[M];
int nxt[M];

//DSU
int parent[N];

int Find(int x){
    return (x == parent[x]) ? x : parent[x] = Find(parent[x]);
}

void Union(int u, int v){
    int x = Find(u);
    int y = Find(v);
    if (x!=y){
        parent[y] = x;
    }
}

/*Solution*/
void solve(){
    cin >> n;
    int mx = 0;
    int mn = LLINF;
    int cnt = 0;
    for (int i=1; i<=n; i++){
        int x;
        cin >> x;
        if (!vis[x]){
            ++cnt;
            vis[x] = cnt;;
            a.push_back(x);
            mx = max(mx,x);
            mn = min(mn,x);
        }
    }
    sort(a.begin(),a.end());
    int rawr = LLINF;
    for (int i=mx+1; i>=1; i--){
        if (vis[i]) rawr = i;
        nxt[i] = rawr;
    }
    for (int idx = 0; idx<(int)a.size(); idx++){
        int x = a[idx];
        for (int i=x; i<=mx; i+=x){
            if (i == x){
                if (nxt[i+1]<i+x){
                    edge.push_back({nxt[i+1]-i,{x,nxt[i+1]}});
                }
            }
            else{
                if (nxt[i]<i+x){
                    edge.push_back({nxt[i]-i,{x,nxt[i]}});
                }
            }
        }
    }
    for (int i=1; i<=cnt; i++){
        parent[i] = i;
    }
    sort(edge.begin(),edge.end());
    int ans = 0;
    for (auto in:edge){
        int w = in.fi;
        int u = vis[in.se.fi];
        int v = vis[in.se.se];

//        cout << in.se.fi << " " << in.se.se << " " << w << endl;

        if (Find(u)!=Find(v)){
            ans+=w;
            Union(u,v);
        }
    }
    cout << ans;
}

/*Driver Code*/
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    TestCases(0);

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 83292 KB Output is correct
2 Correct 43 ms 88796 KB Output is correct
3 Correct 15 ms 83488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 145 ms 81268 KB Output is correct
3 Correct 16 ms 83744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 83548 KB Output is correct
2 Correct 13 ms 82320 KB Output is correct
3 Correct 14 ms 83548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 45764 KB Output is correct
2 Correct 551 ms 120212 KB Output is correct
3 Correct 205 ms 69556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 25352 KB Output is correct
2 Correct 264 ms 115172 KB Output is correct
3 Correct 144 ms 37824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 338 ms 119212 KB Output is correct
2 Correct 679 ms 218772 KB Output is correct
3 Correct 195 ms 71088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 20812 KB Output is correct
2 Correct 599 ms 217504 KB Output is correct
3 Correct 179 ms 69052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 209696 KB Output is correct
2 Runtime error 439 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 231 ms 209192 KB Output is correct
2 Runtime error 490 ms 786584 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 136776 KB Output is correct
2 Runtime error 428 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -