//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;
upd(a[cur.ss]);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
4044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
134 ms |
432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
4352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3994 ms |
10828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
348 ms |
5324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5035 ms |
11752 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2489 ms |
3616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5069 ms |
47032 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5060 ms |
46176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
842 ms |
36220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |