#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int N, A[1 << 18];
bool used[10000009][10];
bool used3[1009][10];
bool used4[10009][10];
void init() {
for (int i = 0; i < 10000000; i++) {
int cx = i;
while (cx >= 1) {
used[i][cx % 10] = true;
cx /= 10;
}
}
for (int i = 0; i < 1000; i++) {
int cx = i;
for (int j = 0; j < 3; j++) {
used3[i][cx % 10] = true;
cx /= 10;
}
}
for (int i = 0; i < 10000; i++) {
int cx = i;
for (int j = 0; j < 4; j++) {
used4[i][cx % 10] = true;
cx /= 10;
}
}
}
bool check(int pos) {
for (int i = 0; i < N; i++) {
if (used[pos][A[i]] == false) return false;
pos++;
}
return true;
}
int cnt[109][10];
long long solve(int dig) {
for (int i = 0; i < 101; i++) {
for (int j = 0; j < 10; j++) cnt[i][j] = 0;
}
int maxt = 0;
for (int i = 0; i < N; i++) {
int V1 = (dig + i) % 1000;
int V2 = (dig + i) / 1000;
maxt = max(maxt, V2);
if (used[V1][A[i]] == true) continue;
cnt[maxt][A[i]] = 1;
}
// ケース 1: 下 3 桁が XYZ の場合
long long m1 = (1LL << 60);
for (int i = 0; i < 900; i++) {
int v[10] = { 0,0,0,0,0,0,0,0,0,0 };
for (int j = 0; j <= maxt; j++) {
for (int k = 0; k < 10; k++) {
if (cnt[j][k] == 1 && used3[i + j][k] == false) v[k] = 1;
}
}
string S = "";
for (int j = 0; j < 10; j++) { if (v[j] == 1) S += ('0' + j); }
if (S[0] == '0') {
if (S.size() == 1) S = "10";
else swap(S[0], S[1]);
}
if (S == "") S = "0";
long long eval = 1000000LL * stoll(S) + 1000LL * i + dig;
m1 = min(m1, eval);
}
// ケース 2: 下 4 桁が X9YZ の場合
long long m2 = (1LL << 60);
for (int i = 0; i < 9000; i++) {
if ((i % 1000) <= 899) continue;
int v[10] = { 0,0,0,0,0,0,0,0,0,0 };
for (int j = 0; j <= maxt; j++) {
for (int k = 0; k < 10; k++) {
if (cnt[j][k] == 1 && used4[i + j][k] == false) v[k] = 1;
}
}
string S = "";
for (int j = 0; j < 10; j++) { if (v[j] == 1) S += ('0' + j); }
if (S[0] == '0') {
if (S.size() == 1) S = "10";
else swap(S[0], S[1]);
}
if (S == "") S = "0";
long long eval = 10000000LL * stoll(S) + 1000LL * i + dig;
m2 = min(m2, eval);
}
return min(m1, m2);
}
int main() {
cin >> N; init();
for (int i = 0; i < N; i++) cin >> A[i];
// ステップ 1. 1000 以下を全探索
for (int i = 1; i <= 1000; i++) {
if (check(i) == true) {
cout << i << endl;
return 0;
}
}
// ステップ 2. そうでない場合
long long minx = (1LL << 60);
for (int i = 0; i <= 999; i++) {
minx = min(minx, solve(i));
}
cout << minx << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
98472 KB |
Output is correct |
2 |
Correct |
161 ms |
98296 KB |
Output is correct |
3 |
Correct |
158 ms |
98296 KB |
Output is correct |
4 |
Correct |
165 ms |
98296 KB |
Output is correct |
5 |
Correct |
164 ms |
98296 KB |
Output is correct |
6 |
Correct |
162 ms |
98296 KB |
Output is correct |
7 |
Correct |
163 ms |
98300 KB |
Output is correct |
8 |
Correct |
164 ms |
98416 KB |
Output is correct |
9 |
Correct |
169 ms |
98296 KB |
Output is correct |
10 |
Correct |
168 ms |
98296 KB |
Output is correct |
11 |
Correct |
165 ms |
98296 KB |
Output is correct |
12 |
Correct |
168 ms |
98364 KB |
Output is correct |
13 |
Correct |
158 ms |
98296 KB |
Output is correct |
14 |
Correct |
158 ms |
98296 KB |
Output is correct |
15 |
Correct |
159 ms |
98308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
98296 KB |
Output is correct |
2 |
Correct |
160 ms |
98296 KB |
Output is correct |
3 |
Correct |
166 ms |
98424 KB |
Output is correct |
4 |
Correct |
164 ms |
98296 KB |
Output is correct |
5 |
Correct |
162 ms |
98296 KB |
Output is correct |
6 |
Correct |
159 ms |
98296 KB |
Output is correct |
7 |
Correct |
312 ms |
98328 KB |
Output is correct |
8 |
Correct |
161 ms |
98296 KB |
Output is correct |
9 |
Correct |
157 ms |
98296 KB |
Output is correct |
10 |
Correct |
163 ms |
98352 KB |
Output is correct |
11 |
Correct |
408 ms |
98300 KB |
Output is correct |
12 |
Correct |
161 ms |
98296 KB |
Output is correct |
13 |
Correct |
161 ms |
98296 KB |
Output is correct |
14 |
Correct |
164 ms |
98296 KB |
Output is correct |
15 |
Correct |
159 ms |
98296 KB |
Output is correct |
16 |
Correct |
166 ms |
98424 KB |
Output is correct |
17 |
Correct |
158 ms |
98296 KB |
Output is correct |
18 |
Correct |
410 ms |
98296 KB |
Output is correct |
19 |
Incorrect |
436 ms |
98296 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
98168 KB |
Output is correct |
2 |
Correct |
580 ms |
98552 KB |
Output is correct |
3 |
Correct |
580 ms |
98424 KB |
Output is correct |
4 |
Incorrect |
642 ms |
98368 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
98296 KB |
Output is correct |
2 |
Correct |
163 ms |
98292 KB |
Output is correct |
3 |
Correct |
158 ms |
98296 KB |
Output is correct |
4 |
Correct |
164 ms |
98296 KB |
Output is correct |
5 |
Execution timed out |
1058 ms |
98424 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |