답안 #489879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
489879 2021-11-25T02:39:19 Z 8e7 Sirni (COCI17_sirni) C++17
56 / 140
5000 ms 47032 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);
		}
	}
	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 4044 KB Output is correct
2 Correct 60 ms 2484 KB Output is correct
3 Correct 23 ms 4292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 428 KB Output is correct
2 Correct 633 ms 868 KB Output is correct
3 Correct 25 ms 4300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 4356 KB Output is correct
2 Correct 20 ms 2856 KB Output is correct
3 Correct 22 ms 4356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2631 ms 10932 KB Output is correct
2 Execution timed out 5043 ms 11868 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 5416 KB Output is correct
2 Correct 560 ms 4576 KB Output is correct
3 Correct 2885 ms 7960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4027 ms 11568 KB Output is correct
2 Execution timed out 5058 ms 11244 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1838 ms 3784 KB Output is correct
2 Execution timed out 5028 ms 10144 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5064 ms 47032 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5067 ms 46156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 634 ms 36224 KB Output is correct
2 Execution timed out 5065 ms 36528 KB Time limit exceeded
3 Halted 0 ms 0 KB -