#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
using ll = long long;
const int MAXN = 1e5 + 123;
const ll INF = 1e18 + 177013;
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 *= 10;
ans += t;
if (incl[0]) {
ans *= 10;
incl[0] = 0;
}
}
if (ans == 0 && incl[0])
return 10;
return ans;
}
ll get_best_nonzero(bitset<10> incl) {
return max(1ll, get_best(incl));
}
ll get_best_conseq(bitset<10> incl0, bitset<10> incl1) {
if (incl0 == 0 && incl1 == 0)
return 0;
//cout << "A" << endl;
ll ans = INF;
for (int d = 0; d < 10; ++d) {
bitset<10> nincl0 = incl0;
bitset<10> nincl1 = incl1;
nincl0[d] = nincl1[(d + 1) % 10] = 0;
if (d == 0 && incl0[0]) {
ans = min(ans, get_best_nonzero(nincl0 | nincl1) * 10);
ans = min(ans, get_best(incl0 | nincl1) * 10);
} else if (d == 9 && (incl0[9] || incl1[0])) {
ans = min(ans, get_best_conseq(nincl0, nincl1) * 10 + 9);
} else {
ans = min(get_best(nincl0 | nincl1) * 10 + d, ans);
}
}
//if (incl0 == 0 && incl1.count() == 1 && incl1[1]) {
//cout << "HYPE " << ans << endl;
//}
//cout << "B" << endl;
return ans;
}
ll get_best_conseq_nonzero(bitset<10> incl0, bitset<10> incl1) {
ll ans = get_best_conseq(incl0, incl1);
if (ans > 0)
return ans;
if (incl0 == 0 && incl1 == 0)
return 1;
return 10;
}
int dgseq[MAXN];
bitset<10> dgs[MAXN];
bitset<10> dgsmod[MAXN];
bitset<10> incl0[MAXN];
bitset<10> incl0mod[MAXN];
bitset<10> incl1[MAXN];
signed main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> dgseq[i];
int m = 10;
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;
}
dgsmod[t] = dgs[t];
if (t * 10 < m)
dgsmod[t][0] = 1;
}
ll ans = INF;
for (int d = 0; d < 10; ++d) {
vector<int> rdgseq;
for (int t = m - 1; t >= 0; --t) {
if (m - t - 1 < n && dgseq[m - t - 1] == d) {
rdgseq.push_back(m - t - 1);
swap(rdgseq.back(), rdgseq[rand() % rdgseq.size()]);
}
for (auto k : rdgseq) {
if (!dgs[t + k][d]) {
incl0[t][d] = 1;
break;
}
}
for (auto k : rdgseq) {
if (!dgsmod[t + k][d]) {
incl0mod[t][d] = 1;
break;
}
}
}
}
for (int d = 0; d < 10; ++d) {
vector<int> rdgseq;
for (int t = 0; t < m; ++t) {
if (m - t < n && dgseq[m - t] == d) {
rdgseq.push_back(m - t);
swap(rdgseq.back(), rdgseq[rand() % rdgseq.size()]);
}
for (auto k : rdgseq) {
if (!dgsmod[t + k - m][d])
incl1[t][d] = 1;
}
}
}
for (int t = 0; t < m; ++t) {
//for (int k = 0; k < n && t + k < m; ++k) {
//if (!dgs[t + k][dgseq[k]])
//incl0[dgseq[k]] = 1;
//if (!dgsmod[t + k][dgseq[k]])
//incl0mod[dgseq[k]] = 1;
//}
//for (int k = m - t; k < n; ++k) {
//if (!dgsmod[t + k - m][dgseq[k]])
//incl1[t][dgseq[k]] = 1;
//}
ans = min(ans, get_best_conseq(incl0[t], incl1[t]) * m + t);
ans = min(ans, get_best_conseq_nonzero(incl0mod[t], incl1[t]) * m + t);
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
312 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
312 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
304 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
312 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
424 ms |
3632 KB |
Output is correct |
3 |
Correct |
411 ms |
3520 KB |
Output is correct |
4 |
Correct |
93 ms |
3540 KB |
Output is correct |
5 |
Correct |
93 ms |
3604 KB |
Output is correct |
6 |
Correct |
191 ms |
796 KB |
Output is correct |
7 |
Execution timed out |
1071 ms |
4312 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
2 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Execution timed out |
1077 ms |
4376 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |