답안 #485251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485251 2021-11-06T17:23:33 Z BackNoob Sirni (COCI17_sirni) C++14
84 / 140
1027 ms 786436 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1) 
#define TASK "task"
#define sz(s) (int) (s).size()

using namespace std;
const int mxN = 1e5 + 227;
const int mxM = 1e7 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

struct DSU{
	vector<int> lab;
	DSU(){}
	DSU(int n) 
	{
		lab.resize(n + 7 , -1);
	}
	int root(int u) {return lab[u] < 0 ? u : lab[u] = root(lab[u]);}
	bool union_root(int u , int v)
	{
		u = root(u);
		v = root(v);
		if(u == v) return false;
		if(lab[u] > lab[v]) swap(u , v);
		lab[u] += lab[v];
		lab[v] = u;
		return true;
	}
} dsu;

struct item{
	int u , v , c;
	bool operator<(const item &a) {
		return c < a.c;
	}
};

int a[mxN];
int best[mxM];
int MAX = 1e7;

vector<item> edges;

void solve()
{
	memset(best , 0x3f , sizeof(best));
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i++) {
		int x;
		cin >> x;
		best[x] = x;
	}

	dsu = DSU(MAX);

	
	for(int i = MAX ; i >= 1 ; i--) minimize(best[i] , best[i + 1]);

	for(int i = 1 ; i <= MAX ; i++) {
		if(best[i] != i) continue;
		if(best[i + 1] != best[0]) edges.pb({i , best[i + 1] , best[i + 1] % i});
		for(int j = i ; j <= MAX ; j += i) {
			if(best[j] == best[0]) break;
			edges.pb({i , best[j] , best[j] % i});
		}
	}

	sort(edges.begin() , edges.end());

	ll res = 0;
	for(auto it : edges) {
		if(dsu.union_root(it.u , it.v) == true) {
			res += it.c;
		}
	}
	cout << res;

}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen(TASK".inp" , "r" , stdin);
    //freopen(TASK".out" , "w" , stdout);
    
    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 78724 KB Output is correct
2 Correct 229 ms 127932 KB Output is correct
3 Correct 67 ms 79012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 78796 KB Output is correct
2 Runtime error 830 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 78660 KB Output is correct
2 Correct 62 ms 78668 KB Output is correct
3 Correct 66 ms 78856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 103596 KB Output is correct
2 Correct 367 ms 177784 KB Output is correct
3 Correct 211 ms 128632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 81864 KB Output is correct
2 Correct 250 ms 128524 KB Output is correct
3 Correct 127 ms 103996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 225 ms 128256 KB Output is correct
2 Correct 444 ms 177828 KB Output is correct
3 Correct 189 ms 103980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 85012 KB Output is correct
2 Correct 489 ms 177812 KB Output is correct
3 Correct 208 ms 128636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 215 ms 103608 KB Output is correct
2 Runtime error 936 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 103584 KB Output is correct
2 Runtime error 937 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 81940 KB Output is correct
2 Runtime error 1027 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -