Submission #489882

# Submission time Handle Problem Language Result Execution time Memory
489882 2021-11-25T02:43:05 Z 8e7 Sirni (COCI17_sirni) C++17
112 / 140
5000 ms 56808 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], ind[maxc];
bool found[maxn];
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> a;
set<int> se;
int n;
void upd(int x) {
	auto checkbig = [&] (int i) {
		auto it = se.lower_bound(i);
		if (it != se.end()) {
			int id = ind[*it];
			if (a[id] - i < dis[id]) {
				dis[id] = a[id] - i;	
				pq.push({dis[id], id});
			}
		}
	};
	auto checksmall = [&] (int i) {
		auto it = se.upper_bound(i);
		if (it != se.begin()) {
			it = prev(it);
			int id = ind[*it];
			if (x % a[id] < dis[id]) {
				dis[id] = x % a[id];
				pq.push({dis[id], id});
			}
		}
	};
	for (int i = (maxc-1) - ((maxc-1) % x);i >= x;i -= x) {
		if (!rig[i]) {
			rig[i] = 1;
			checkbig(i);
		}
	}
	checkbig(x);
	int tmp = x;
	for (int i = 1;i * i <= x;i++) {
		tmp = x / i;
		if (!lef[tmp]) {
			checksmall(tmp);	
		}	
	}
	for (;tmp >= 1;tmp--) {
		if (!lef[tmp] && ind[tmp]) {
			//lef[tmp] = 1;
			if (x % tmp < dis[ind[tmp]]) {
				dis[ind[tmp]] = x % tmp;
				pq.push({x%tmp, ind[tmp]}); 
			}	
		}	
	}
}
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, se.insert(a[i]);
		ind[a[i]] = i;
	}
	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;
		se.erase(a[cur.ss]);
		upd(a[cur.ss]);	
	}
	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 12620 KB Output is correct
2 Correct 92 ms 12208 KB Output is correct
3 Correct 28 ms 14028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 10204 KB Output is correct
2 Correct 209 ms 10700 KB Output is correct
3 Correct 29 ms 14008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 13260 KB Output is correct
2 Correct 21 ms 8524 KB Output is correct
3 Correct 28 ms 14112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2101 ms 20428 KB Output is correct
2 Correct 2140 ms 21036 KB Output is correct
3 Correct 2101 ms 19980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 264 ms 15140 KB Output is correct
2 Correct 424 ms 14456 KB Output is correct
3 Correct 1666 ms 17660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2245 ms 21368 KB Output is correct
2 Correct 2345 ms 20436 KB Output is correct
3 Correct 2269 ms 21016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 870 ms 13364 KB Output is correct
2 Correct 1616 ms 19412 KB Output is correct
3 Correct 2035 ms 20404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5064 ms 56808 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5060 ms 55956 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 607 ms 46020 KB Output is correct
2 Correct 2549 ms 46360 KB Output is correct
3 Correct 2061 ms 20620 KB Output is correct