#include <bits/stdc++.h>
using namespace std;
random_device rd;
knuth_b gen(rd());
const int MAXN = 1e5 + 10;
int K,vetor[MAXN],freq[10][2];
int pretest[20];
int check(int ini){
for(int i = ini,ptr = 1;ptr <= K;i++,ptr++){
if(i == 0 && vetor[ptr] == 0) continue;
int copia = i;
while(copia != 0 && copia % 10 != vetor[ptr]) copia /= 10;
if(copia == 0) return 0;
}
return 1;
}
int pretesting(int ini){
for(int j = 0;j<19;j++){
int ptr = pretest[j];
int i = vetor[ptr];
if(i == 0 && vetor[ptr] == 0) continue;
int copia = i;
while(copia != 0 && copia % 10 != vetor[ptr]) copia /= 10;
if(copia == 0) return 0;
}
return 1;
}
void adiciona(int v){
while(v != 0){
freq[v % 10][0]++;
v /= 10;
}
}
void tira(int v){
while(v != 0){
freq[v % 10][0]--;
v /= 10;
}
}
int prune(int v){
if(!pretesting(v)) return 1;
for(int i = 0;i<=9;i++){
if(freq[i][0] < freq[i][1]) return 0;
}
return 0;
}
int main(){
cin >> K;
for(int i = 1;i<=K;i++){
cin >> vetor[i];
freq[vetor[i]][1]++;
}
for(int i = 0;i<19;i++){
int aleatorio = 1 + (gen() % K);
pretest[i] = aleatorio;
}
if(K <= 1000){
for(int i = 1;i<=K;i++) adiciona(i);
for(int i = 1;i<=1000000;i++){
if(prune(i)){
adiciona(i+K);
tira(i);
}
if(check(i)){
cout << i << endl;
return 0;
}
adiciona(i+K);
tira(i);
}
}
if(vetor[1] == 9){
int geratriz = 8;
while(true){
if(check(geratriz + 1)){
cout << geratriz + 1 << endl;
return 0;
}
geratriz *= 10;
geratriz += 8;
}
}
else if(vetor[1] == 0){
int ini = 1;
while(true){
if(check(ini)){
cout << ini << endl;
return 0;
}
ini *= 10;
}
}
else{
int ini = vetor[1];
while(true){
if(check(ini)){
cout << ini << endl;
return 0;
}
ini *= 10;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
2 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
440 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
744 KB |
Output is correct |
2 |
Correct |
2 ms |
744 KB |
Output is correct |
3 |
Correct |
2 ms |
744 KB |
Output is correct |
4 |
Correct |
1 ms |
744 KB |
Output is correct |
5 |
Correct |
1 ms |
744 KB |
Output is correct |
6 |
Correct |
1 ms |
744 KB |
Output is correct |
7 |
Correct |
3 ms |
744 KB |
Output is correct |
8 |
Correct |
2 ms |
744 KB |
Output is correct |
9 |
Correct |
2 ms |
744 KB |
Output is correct |
10 |
Correct |
1 ms |
744 KB |
Output is correct |
11 |
Execution timed out |
1073 ms |
744 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
744 KB |
Output is correct |
2 |
Correct |
4 ms |
744 KB |
Output is correct |
3 |
Correct |
4 ms |
744 KB |
Output is correct |
4 |
Correct |
5 ms |
744 KB |
Output is correct |
5 |
Correct |
4 ms |
744 KB |
Output is correct |
6 |
Correct |
3 ms |
744 KB |
Output is correct |
7 |
Correct |
15 ms |
876 KB |
Output is correct |
8 |
Correct |
10 ms |
876 KB |
Output is correct |
9 |
Correct |
22 ms |
1004 KB |
Output is correct |
10 |
Correct |
21 ms |
1004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1004 KB |
Output is correct |
2 |
Correct |
2 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Execution timed out |
1066 ms |
1004 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |