#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define int long long
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10 , inf = 1e17 + 10 ;
int a10[18] ;
int dfs(vector <int> vec, int ok){
if(vec.size() == 0)return ok ;
if(vec.size() == 1){
if(vec[0] == 0){
return ok ;
}
if(vec[0] == 1){
return 10 ;
}
vector <int> vj ;
for(int i = 0 ;i < 10 ; i++){
if(vec[0]>>i&1){
vj.pb(i) ;
}
}
if(vj[0] == 0){
swap(vj[0], vj[1]);
}
int ans = 0;
for(int i= 0 ; i < vj.size() ; i++){
ans *= 10 ;
ans += vj[i] ;
}
return ans ;
}
int ans = inf ;
for(int i = 0 ; i < 10 ; i++){
int t =0 , k = i ;
vector <int> vec2 ;
for(int j = 0; j < vec.size() ; j++){
if(k == 10){
k =0 ;
vec2.pb(t) ;
t = 0 ;
}
if(vec[j]>>k&1){
vec[j] -= (1<<k) ;
t |= vec[j] ;
vec[j]+= (1<<k) ;
}else{
t |= vec[j] ;
}
k++;
}
vec2.pb(t) ;
if(vec2 == vec && ok == (i==0)){
continue ;
}
ans = min(ans , dfs(vec2 , (i==0) * (vec[0]%2)) * 10 + i ) ;
}
return ans ;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0) ;
vector<int>vec ;
int n ;cin >> n ;
for(int i = 1; i <= n; i++){
int x;
cin >> x ;
vec.pb((1<<x)) ;
}
cout << dfs(vec , 1) ;
}
/*
4 7 8 9 1
4 7 0 0 0
4 8 0 0 0
4 7 8 0 1
*/
Compilation message
sequence.cpp: In function 'long long int dfs(std::vector<long long int>, long long int)':
sequence.cpp:35:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i= 0 ; i < vj.size() ; i++){
| ~~^~~~~~~~~~~
sequence.cpp:45:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j = 0; j < vec.size() ; j++){
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
548 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
15 ms |
600 KB |
Output is correct |
3 |
Correct |
15 ms |
604 KB |
Output is correct |
4 |
Correct |
12 ms |
856 KB |
Output is correct |
5 |
Correct |
12 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
60 ms |
1752 KB |
Output is correct |
8 |
Correct |
84 ms |
1240 KB |
Output is correct |
9 |
Correct |
93 ms |
2448 KB |
Output is correct |
10 |
Correct |
82 ms |
2444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
147 ms |
1388 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
101 ms |
2456 KB |
Output is correct |
12 |
Correct |
159 ms |
2264 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
600 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
15 ms |
604 KB |
Output is correct |
28 |
Correct |
15 ms |
604 KB |
Output is correct |
29 |
Correct |
12 ms |
604 KB |
Output is correct |
30 |
Correct |
12 ms |
476 KB |
Output is correct |
31 |
Correct |
7 ms |
604 KB |
Output is correct |
32 |
Correct |
61 ms |
1596 KB |
Output is correct |
33 |
Correct |
63 ms |
1244 KB |
Output is correct |
34 |
Correct |
84 ms |
2264 KB |
Output is correct |
35 |
Correct |
81 ms |
2452 KB |
Output is correct |
36 |
Correct |
231 ms |
1756 KB |
Output is correct |
37 |
Correct |
306 ms |
2640 KB |
Output is correct |
38 |
Correct |
180 ms |
1500 KB |
Output is correct |
39 |
Correct |
315 ms |
2264 KB |
Output is correct |
40 |
Correct |
320 ms |
2448 KB |
Output is correct |