답안 #547285

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547285 2022-04-10T09:03:55 Z AA_Surely Sirni (COCI17_sirni) C++14
84 / 140
759 ms 319444 KB
#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cout << "WTF" << endl

#define IOS 		ios::sync_with_stdio(false); cin.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()

using namespace std;
typedef long long 		LL;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;

const int N = 1e5 + 7;
const int A = 1e7 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;

struct Edge {
    int u, v, w;

    inline bool operator < (const Edge other) {
        return w < other.w;
    }
} eset[A];

int n, era[A];
int par[N];

int grand(int x) {
    if (par[x] < 0) return x;
    return par[x] = grand(par[x]);
}

inline bool merge(int a, int b) {
    a = grand(a); b = grand(b);
    if (a == b) return 0;

    if (par[a] > par[b]) swap(a, b);
    par[a] += par[b];
    par[b] = a;

    return 1;
}

int main() {
    IOS;

    fill(era, era + A, -1);
    
    cin >> n;
    VI ns(n);
    F0R(i, n) cin >> ns[i];

    sort(ALL(ns));
    ns.resize(unique(ALL(ns)) - ns.begin());
    n = ns.size();

    F0R(i, n) era[ ns[i] ] = i;
    R0F(i, A - 1) if (era[i] == -1)
        era[i] = era[i + 1];
    
    int fre = 0;
    F0R(i, n) for(int j = ns[i]; j < A; j += ns[i]) if (era[j] != -1) {
        eset[fre].u = i; eset[fre].v = era[j];
        eset[fre].w = ns[ era[j] ] - j;
        fre++;

        if (era[j + 1] != -1 && era[j + 1] != era[j]) {
            eset[fre].u = i; eset[fre].v = era[j + 1];
            eset[fre].w = ns[ era[j + 1] ] - j;
            fre++;
        }
    }

    sort(eset, eset + fre);
    fill(par, par + n, -1);
    
    LL ans = 0;
    F0R(i, fre) if (merge(eset[i].u, eset[i].v)) ans += eset[i].w;
    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 39460 KB Output is correct
2 Correct 287 ms 71976 KB Output is correct
3 Correct 42 ms 39720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 39500 KB Output is correct
2 Runtime error 225 ms 317940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 39468 KB Output is correct
2 Correct 39 ms 39476 KB Output is correct
3 Correct 40 ms 39592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 54728 KB Output is correct
2 Correct 551 ms 95080 KB Output is correct
3 Correct 297 ms 68148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 41812 KB Output is correct
2 Correct 408 ms 67148 KB Output is correct
3 Correct 243 ms 55704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 351 ms 71000 KB Output is correct
2 Correct 721 ms 116956 KB Output is correct
3 Correct 258 ms 66104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 45132 KB Output is correct
2 Correct 759 ms 118128 KB Output is correct
3 Correct 252 ms 66880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 57272 KB Output is correct
2 Runtime error 240 ms 319444 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 63360 KB Output is correct
2 Runtime error 244 ms 319292 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 41964 KB Output is correct
2 Runtime error 321 ms 319248 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -