Submission #485249

# Submission time Handle Problem Language Result Execution time Memory
485249 2021-11-06T16:48:16 Z BackNoob Sirni (COCI17_sirni) C++14
0 / 140
197 ms 89520 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()
{
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i++) cin >> a[i];

	dsu = DSU(n);
	sort(a + 1 , a + 1 + n);

	
	int r = n;
	for(int i = MAX ; i >= 1 ; i--) {
		while(a[r - 1] >= i && r >= 1) {
			--r;
		}
		if(a[r] >= i) best[i] = r;
		else best[i] = -1;
	} 

	for(int i = 1 ; i <= n ; i++) {
		if(a[i] == a[i - 1] && i >= 1) continue;
		for(int j = a[i] ; j <= MAX ; j += a[i]) {
			if(best[j] == -1) break;
			edges.pb({i , best[j] , a[best[j]] % a[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 Incorrect 30 ms 39524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 39584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 39616 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 114 ms 64932 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 41 ms 42656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 197 ms 89520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 46004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 151 ms 64960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 170 ms 64988 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 45 ms 42808 KB Output isn't correct
2 Halted 0 ms 0 KB -