COFO::Cofo Educational Round #147

Problem A : Matching

point

Design

Big O(time)

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;

void solve() {
    string s; cin >> s;
    int cntq = count(s.begin(), s.end(), '?');
    if (cntq == 0 || s[0] == '0') {
        if (s[0] == '0')    cout << "0\n";
        else cout << 1 << '\n';
    } else {
        int ans = 1;
        rep(i, 0, sz(s)) if (s[i] == '?'){
            if (i == 0) ans *= 9;
            else ans *= 10;
        }
        cout << ans << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int tc; cin >> tc; while(tc--)
        solve();
    return 0;
}

Problem B : Sort the Subarray

Point

Design

Big O(time)

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;

void solve() {
    int n; cin >> n;
    vector<int> a(n), b(n);
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n) cin >> b[i];
    
    // b에서 가장 긴 non-decreasing order 찾기
    // [l:r] 사이에 포함되지 않았지만, 그냥 sort 된 것처럼 보이는 인덱스는 ? 얘도 결국 l, r 에 포함될텐데
    // 여전히 상관없음

    int le = 1e9, ri = -1;
    rep(i, 0, n) {
        if (a[i] != b[i]) {
            le = min(le, i);
            ri = max(ri, i);
        }
    }
    int prv = ri;
    rep(i, prv+1, n) {
        if (b[i-1] <= b[i]) ri++; // prv >= 1
        else break;
    }
    
    prv = le;
    r_rep(i, prv-1, -1) {
        if (b[i] <= b[i+1]) le--;
        else break;
    }
    cout << le + 1 << " " << ri + 1 << '\n';
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int tc; cin >> tc; while(tc--)
        solve();
    return 0;
}

Problem C : Tear it apart

Point

Design

Big O(time)

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;

string ors;
bool _possible(int cnt, char dst) {
    int ops = 0;
    string s = ors, t = "";
    
    int prv = -2;
    while(sz(s) > 0 && (count(all(s), dst) != sz(s))) {
        rep(i, 0, sz(s)) {
            if (s[i] == dst) {
                t += s[i];
            } else {
                if (prv != i-1) {
                    // do nothing. erased
                    prv = i;
                } else {
                    // add to the string. can't delete it
                    t += s[i];
                }
            }
        }
        ops++;
        s = t;
        t = "";
    }
    return ops <= cnt;
}
int bs(int le, int ri, int x) {
    int ret = ri;
    char c = x + 'a';
    while(le <= ri) {
        int mid = (le + ri) / 2;
        if (_possible(mid, c)) {
            ret = min(ret, mid);
            ri = mid - 1;
        } else le = mid + 1;
    }
    return ret;
}

void solve() {
    cin >> ors;
    
    int ans = 30;
    rep(i, 0, 26) {
        if (count(all(ors), i + 'a') == 0) continue;
        int ret = bs(0, 30, i);
        ans = min(ans, ret);
    }
    cout << ans << '\n';
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int tc; cin >> tc; while(tc--)
        solve();
return 0;
}

Problem D : Black Cells

Point

Design

Big O(time)

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;

void solve() {
    int n; ll k; cin >> n >> k;
    vector<ll> le(n), ri(n);
    rep(i, 0, n) cin >> le[i];
    rep(i, 0, n) cin >> ri[i];
    
    int cnt1 = 0, cnt2 = 0;
    
    ll ans = 1e18, sum = 0;
    rep(i, 0, n) {
        ll len = ri[i] - le[i] + 1;
        if (len >= 2) {
            cnt2++;
            sum += len;
            if (sum >= k) {
                ans = min(ans, ri[i] + 2 * cnt2 - (sum - k));
            }
        }
        else {
            cnt1++;
        }
        ll hasto = max(0LL, k - sum);
        if (cnt1 >= hasto)
            ans = min(ans, ri[i] + 2 * (cnt2 + hasto));
    }
    
    
    if (ans == 1e18) ans = -1;
    cout << ans << '\n';
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int tc; cin >> tc; while(tc--)
        solve();
    return 0;
}