Submission #485252

# Submission time Handle Problem Language Result Execution time Memory
485252 2021-11-06T17:26:12 Z BackNoob Sirni (COCI17_sirni) C++14
140 / 140
3001 ms 473676 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;
			if(j - i < i || best[j - i] != best[j]) 
				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;
}
# Verdict Execution time Memory Grader output
1 Correct 61 ms 78660 KB Output is correct
2 Correct 98 ms 81720 KB Output is correct
3 Correct 62 ms 78892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 78740 KB Output is correct
2 Correct 265 ms 79444 KB Output is correct
3 Correct 62 ms 78832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 78732 KB Output is correct
2 Correct 61 ms 78664 KB Output is correct
3 Correct 61 ms 78788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 103436 KB Output is correct
2 Correct 306 ms 128156 KB Output is correct
3 Correct 153 ms 103600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 81852 KB Output is correct
2 Correct 232 ms 128184 KB Output is correct
3 Correct 123 ms 103544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 128052 KB Output is correct
2 Correct 371 ms 177352 KB Output is correct
3 Correct 152 ms 103472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 84916 KB Output is correct
2 Correct 415 ms 177412 KB Output is correct
3 Correct 143 ms 103460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 103336 KB Output is correct
2 Correct 2095 ms 473676 KB Output is correct
3 Correct 232 ms 104088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 103268 KB Output is correct
2 Correct 2961 ms 473492 KB Output is correct
3 Correct 254 ms 104108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 81688 KB Output is correct
2 Correct 3001 ms 473384 KB Output is correct
3 Correct 170 ms 104048 KB Output is correct