This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
long long pw[18];
long long res = 1e18;
bool in[10];
void go(vector<int> vec, long long cur, int cnt, bool zero) {
if (vec.size() == 1) {
vector<int> buf;
for (int i = 9; i >= 0; --i) {
if (vec[0] >> i & 1) buf.push_back(i);
}
if (buf.size() == 1 && buf[0] == 0) zero = 1;
else if (buf.size() && buf.back() == 0) {
swap(buf[buf.size() - 2], buf[buf.size() - 1]), zero = 0;
}
else if (buf.size()) zero = 0;
for (auto i : buf) cur += i * pw[cnt++];
if (zero) cur += pw[cnt];
res = min(res, cur);
return;
}
vector<int> buf;
int j = 0, mask = 0;
bool flag = 0;
for (auto i : vec) {
if (!j && !flag) {}
else if (i >> j & 1) i ^= 1 << j;
mask |= i, j++;
if (j == 10) {
buf.push_back(mask), mask = j = 0;
flag = 1;
}
}
if (j) buf.push_back(mask);
if (buf.size() == 1 && !buf[0]) res = min(res, cur);
for (int i = 0; i < 10; ++i) {
vector<int> buf;
int k = i, mask = 0;
for (auto j : vec) {
if (j >> k & 1) j ^= 1 << k;
mask |= j, k++;
if (k == 10) {
buf.push_back(mask), mask = k = 0;
}
}
if (k) buf.push_back(mask);
if (buf != vec) {
go(buf, cur + i * pw[cnt], cnt + 1, !i);
}
}
}
int main() {
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> vec;
for (int i = 0; i < n; ++i) {
int x; cin >> x, vec.push_back(1 << x);
}
pw[0] = 1;
for (int i = 1; i < 18; ++i) pw[i] = pw[i - 1] * 10;
go(vec, 0, 0, 0);
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |