이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bitset>
using namespace std;
using ll = long long;
const int MAXN = 1e5 + 123;
const ll INF = 1e18 + 177013;
int dgseq[MAXN];
bitset<10> dgs[MAXN];
bool in(int x, int n) {
while (n > 0) {
if (n % 10 == x)
return true;
n /= 10;
}
return false;
}
ll get_best(bitset<10> incl) {
ll ans = 0;
for (int t = 1; t < 10; ++t) {
if (!incl[t])
continue;
ans += t;
ans *= 10;
if (incl[0]) {
ans *= 10;
incl[0] = 0;
}
}
return ans;
}
ll get_best_conseq(bitset<10> incl0, bitset<10> incl1) {
if (incl0 == 0 && incl1 == 0)
return 0;
ll ans = INF;
for (int d = 0; d < 9; ++d) {
bitset<10> nincl0 = incl0;
bitset<10> nincl1 = incl1;
nincl0[d] = nincl1[d + 1] = 0;
ans = min(get_best(nincl0 | nincl1) * 10 + d, ans);
}
if (incl0[9] || incl1[0]) {
incl0[9] = incl1[0] = 0;
ans = min(ans, get_best_conseq(incl0, incl1) * 10 + 9);
}
return ans;
}
signed main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> dgseq[i];
int m = 1;
while (m < n)
m *= 10;
for (int t = 0; t < m; ++t) {
int x = t;
while (x > 0) {
dgs[t][x % 10] = 1;
x /= 10;
}
}
ll ans = INF;
for (int t = 0; t < m; ++t) {
bitset<10> incl0;
bitset<10> incl1;
for (int k = 0; k < n; ++k) {
if (dgs[(t + k) % m][dgseq[k]])
continue;
if (t + k < m)
incl0[dgseq[k]] = 1;
else
incl1[dgseq[k]] = 1;
}
ans = min(ans, get_best_conseq(incl0, incl1) * m + t);
}
cout << ans << '\n';
}
# | 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... |