Submission #489872

# Submission time Handle Problem Language Result Execution time Memory
489872 2021-11-25T02:19:17 Z 8e7 Sirni (COCI17_sirni) C++17
0 / 140
5000 ms 26652 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <assert.h>
#include <cmath>
#include <iomanip>
#include <random>
using namespace std;
void debug(){cout << endl;};
template<class T, class ...U> void debug(T a, U ... b){cout << a << " ", debug(b ...);};
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
};
#define ll long long
#define maxn 100005
#define maxc 10000005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
bool rig[maxc], lef[maxc];
int dis[maxn];
bool found[maxn];
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> a;
int n;
void upd(int x) {
	
	for (int i = x;i < maxc;i += x) {
		if (!rig[i] || 1) {
			rig[i] = 1;
			int ind = lower_bound(a.begin(), a.end(), i) - a.begin();
			if (ind < n && !found[ind] && a[ind] - i < dis[ind]) {
				dis[ind] = a[ind] - i;	
				pq.push({dis[ind], ind});
			}
		}
	}
	int tmp = x;
	for (int i = 1;i * i <= x;i++) {
		tmp = x / i;
		if (!lef[tmp] || 1) {
			lef[tmp] = 1;
			int ind = upper_bound(a.begin(), a.end(), tmp) - a.begin()-1;
			if (ind >= 0 && !found[ind] && x % a[ind] < dis[ind]) {
				dis[ind] = x % a[ind];
				pq.push({dis[ind], ind});
			}	
		}	
	}
	for (;tmp >= 1;tmp--) {
		if (!lef[tmp] || 1) {
			lef[tmp] = 1;
			int ind = upper_bound(a.begin(), a.end(), tmp) - a.begin()-1;
			if (ind >= 0 && !found[ind] && x % a[ind] < dis[ind]) {
				dis[ind] = x % a[ind];
				pq.push({dis[ind], ind});
			}
		}	
	}
}
int main() {
	io
	cin >> n;
	a.resize(n, 0);
	for (int i = 0;i < n;i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	a.resize(int(unique(a.begin(), a.end()) - a.begin()));
	n = a.size();
	dis[0] = 0;
	found[0] = 1;
	for (int i = 1;i < n;i++) dis[i] = 1<<30;
	upd(a[0]);
	int ans = 0;
	while (pq.size()) {
		pii cur = pq.top();pq.pop();
		if (cur.ff != dis[cur.ss] || found[cur.ss]) continue;
		ans += cur.ff;
		found[cur.ss] = 1;
		upd(a[cur.ss]);	
	}
	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Incorrect 45 ms 14532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 92 ms 10144 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 51 ms 15148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3375 ms 13372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 384 ms 11832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4698 ms 13668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 892 ms 11428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5042 ms 22936 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5059 ms 26652 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1359 ms 20528 KB Output isn't correct
2 Halted 0 ms 0 KB -