답안 #66162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
66162 2018-08-09T22:53:31 Z Benq 수열 (BOI14_sequence) C++11
0 / 100
78 ms 784 KB
#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,3) {
        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.s|b.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 1000*t;
    // abc -> ab(c+1)
    // abc9 -> ab(c+1)0
}

ll test(int st) {
    int x = min(1000-st,K);
    auto a = get(st,0,x-1), b = get(0,x,K-1);
    ll ret = st+solve(a,b);
    // if (st == 47) cout << st << " " << x-1 << " " << a.f << " " << a.s << " " << b.f << " " << b.s << " " << ret << "\n";
    return ret;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> K;
    F0R(i,K) cin >> B[i];
    F0R(i,1000) 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 2 ms 248 KB Output is correct
2 Incorrect 6 ms 360 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 472 KB Output is correct
2 Incorrect 6 ms 604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 732 KB Output is correct
2 Incorrect 78 ms 732 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 732 KB Output is correct
2 Incorrect 7 ms 784 KB Output isn't correct
3 Halted 0 ms 0 KB -