#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
ll ans = INF;
int K, B[MX];
int match(int x, int y) {
F0R(i,5) {
if (x % 10 == y) {
if (y == 0 && x == 0) return 2;
return 1;
}
x /= 10;
}
return 0;
}
pi get(int st, int l, int r) {
int notZero = 0, need = 0;
FOR(i,l,r+1) {
int x = match(st+i-l,B[i]);
if (x == 0) need |= 1<<B[i];
if (x == 2) notZero = 1;
}
return {need,notZero};
}
ll tri(int x, int y) {
if (x == 0) return y;
if (x == 1) x ^= 2;
ll t = 0;
FOR(i,1,10) if (x&(1<<i)) {
t = i;
x ^= 1<<i;
break;
}
F0R(i,10) if (x&(1<<i)) {
t = 10*t+i;
x ^= 1<<i;
}
return t;
}
ll solve(pi a, pi b) { // what about leading zeroes??
ll t = INF;
F0R(i,9) {
int A = min(a.f,a.f^(1<<i));
int B = min(b.f,b.f^(1<<(i+1)));
t = min(t,10*tri(A|B,(a.f&1) || (i == 0 && a.s))+i);
A = min(A,A^(1<<9));
B = min(B,B^(1<<0));
t = min(t,100*tri(A|B,0)+10*i+9);
}
return 100000*t;
}
ll test(int st) {
int x = min(100000-st,K);
auto a = get(st,0,x-1), b = get(0,x,K-1);
ll ret = st+solve(a,b);
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> K;
F0R(i,K) cin >> B[i];
F0R(i,100000) ans = min(ans,test(i));
cout << ans;
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
376 KB |
Output is correct |
2 |
Correct |
639 ms |
512 KB |
Output is correct |
3 |
Correct |
184 ms |
652 KB |
Output is correct |
4 |
Correct |
316 ms |
652 KB |
Output is correct |
5 |
Correct |
53 ms |
652 KB |
Output is correct |
6 |
Correct |
52 ms |
652 KB |
Output is correct |
7 |
Correct |
161 ms |
652 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
652 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
652 KB |
Output is correct |
2 |
Correct |
638 ms |
652 KB |
Output is correct |
3 |
Correct |
180 ms |
652 KB |
Output is correct |
4 |
Correct |
211 ms |
652 KB |
Output is correct |
5 |
Correct |
50 ms |
652 KB |
Output is correct |
6 |
Correct |
56 ms |
652 KB |
Output is correct |
7 |
Execution timed out |
1075 ms |
652 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
652 KB |
Output is correct |
2 |
Execution timed out |
1080 ms |
652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
652 KB |
Output is correct |
2 |
Correct |
649 ms |
748 KB |
Output is correct |
3 |
Correct |
199 ms |
748 KB |
Output is correct |
4 |
Correct |
297 ms |
748 KB |
Output is correct |
5 |
Execution timed out |
1064 ms |
748 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |