/** MIT License Copyright (c) 2018-2019 Vasilev Daniil **/
#include <bits/stdc++.h>
using namespace std;
template<typename T> using v = vector<T>;
#define int long long
typedef long double ld;
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define fs first
#define sc second
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
const long double EPS = 1e-10;
#if 0 //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt" : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin >>
#define put fout <<
#else
#define get cin >>
#define put cout <<
#endif
#define eol put endl
void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg) ;read(args...) ;}
void print(){} template<typename Arg,typename... Args> void print(Arg arg,Args... args){put (arg)<<" ";print(args...);}
int getInt(){int a; get a; return a;}
//code starts here
v<vint> pos;
int ans = inf;
bool has(v<char>& a, int d) { for (int i : a) if (i == d) return 1; return 0; }
int pw[8] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};
void solve(v<v<char>> a, int start, int h = 0, bool lez = 1) {
if (sz(a) == 1) {
sort(a[0].begin(), a[0].end());
int p = 0;
int lz = 0;
rep(i, 0, sz(a[0]))
if (a[0][i] == 0)
lz++;
else {
p = (p * 10) + a[0][i];
for (; lz; lz--)
p = (p * 10);
}
if (lz) {
p = 1;
for (; lz; lz--)
p = (p * 10);
}
if (p == 0 && lez)
p = 1;
p = p * pw[h] + start;
ans = min(ans, p);
return;
}
rep(y, 0, 10 - (sz(a) == 2)) {
v<v<char>> b;
b.pb(v<char>());
int d = y;
rep(i, 0, sz(a)) {
for (int j = 0; j < 10; j++)
if (j != d && has(a[i], j))
b.back().pb(j);
d = (d + 1) % 10;
if (d == 0 && i != sz(a) - 1)
b.pb(v<char>());
}
solve(b, y * pw[h] + start, h + 1, y == 0);
}
}
void run() {
int k;
get k;
v<v<char>> a(k);
rep(i, 0, k) {
int d;
get d;
a[i] = {(char) d};
}
for (int z = 99; z > 0; z -= 10)
rep(i, 0, k) {
int need = a[i][0];
int p = z + i;
while (p) {
if (need == p % 10)
goto nxt;
else
p /= 10;
}
break;
nxt:;
if (i == k - 1)
ans = z;
}
solve(a, {});
put ans;
}
/* 61
5
1 2 3 4 6
*//* 89
2
9 9
*/
signed main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); put fixed << setprecision(12); run(); return 0;}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Incorrect |
7 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Incorrect |
9 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
88 ms |
1772 KB |
Output is correct |
3 |
Correct |
84 ms |
1780 KB |
Output is correct |
4 |
Correct |
91 ms |
1764 KB |
Output is correct |
5 |
Correct |
90 ms |
1800 KB |
Output is correct |
6 |
Incorrect |
68 ms |
1280 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Incorrect |
7 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |