답안 #887067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887067 2023-12-13T15:48:20 Z beaboss Sirni (COCI17_sirni) C++14
140 / 140
1172 ms 633252 KB
// Source: https://oj.uz/problem/view/COCI17_sirni
// 

#include "bits/stdc++.h"

using namespace std;

#define s second
#define f first
#define pb push_back

typedef long long ll;

typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef vector<int> vi;

#define FOR(i, a, b) for (int i = (a); i<b; i++)

bool ckmin(int& a, int b){ return b < a ? a = b, true : false; }

bool ckmax(int& a, int b){ return b > a ? a = b, true : false; }



const int P = 1e7 + 10;

vpii ed[P];
bool vals[P];
struct DSU {
	vector<int> e;
	DSU(int N) { e = vector<int>(N, -1); }

	// get representive component (uses path compression)
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }

	bool same_set(int a, int b) { return get(a) == get(b); }

	int size(int x) { return -e[get(x)]; }

	bool unite(int x, int y) {  // union by size
		x = get(x), y = get(y);
		if (x == y) return false;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y];
		e[y] = x;
		return true;
	}
};

DSU dsu(P);

int nxt[P];

set<int> uni;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;

	FOR(i, 1, n + 1) {
		int k;
		cin >> k;
		vals[k]=true;
		uni.insert(k);
	}

	int largest = -1;
	int cur = largest;

	for (int i = P-1; i >= 0; i--) {
		if (vals[i]) largest=i;
		// if (largest != -1) cout << largest << endl;
		nxt[i] = largest;
	}

	for (auto cur: uni) {

		for (int e = cur; e < P; e += cur) {


			int val = nxt[e];
			if (e == cur) val = nxt[e+1];

			if (val == -1) break;
			
			if (val < e + cur) ed[val % cur].pb({cur, val});


			// cout << cur << val << endl;

			// if (it == uni.begin()) continue;
			// int val = *prev(it, 1);
			// while (val >= e) {
			// 	cout << cur << val << endl;
			// 	ed.pb({val - e, {cur, val}});
			// 	ub = (e + val) / 2;

			// 	if (val == e) break;

			// 	it = uni.upper_bound(ub);
			// 	if (it == uni.begin()) break;
			// 	val = *prev(it, 1);
			// } 
		}
	}


	int sz = 0;

	FOR(c, 0, P) {
		for (auto e: ed[c]) 
			if (dsu.unite(e.f, e.s)) sz += c;
	
	}

	cout << sz << endl;


}












Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:73:6: warning: unused variable 'cur' [-Wunused-variable]
   73 |  int cur = largest;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 317732 KB Output is correct
2 Correct 141 ms 319104 KB Output is correct
3 Correct 109 ms 318032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 315216 KB Output is correct
2 Correct 359 ms 316188 KB Output is correct
3 Correct 106 ms 318036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 317968 KB Output is correct
2 Correct 101 ms 316260 KB Output is correct
3 Correct 92 ms 323440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 331616 KB Output is correct
2 Correct 198 ms 360368 KB Output is correct
3 Correct 148 ms 335812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 319436 KB Output is correct
2 Correct 140 ms 339632 KB Output is correct
3 Correct 136 ms 330768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 174 ms 343588 KB Output is correct
2 Correct 208 ms 375768 KB Output is correct
3 Correct 149 ms 334772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 322484 KB Output is correct
2 Correct 203 ms 368940 KB Output is correct
3 Correct 144 ms 334016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 340816 KB Output is correct
2 Correct 704 ms 544796 KB Output is correct
3 Correct 185 ms 343148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 173 ms 339876 KB Output is correct
2 Correct 1132 ms 633252 KB Output is correct
3 Correct 192 ms 346768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 326484 KB Output is correct
2 Correct 1172 ms 621680 KB Output is correct
3 Correct 154 ms 336580 KB Output is correct