COFO::Cofo Round #863

Problem A : Insert Digit

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, d; cin >> n >> d;
    string s; cin >> s;
    bool f = false;
    rep(i, 0, n) if (s[i] - '0' < d) f = true;
    if (!f) {
        cout << s << d << '\n';
        return;
    }
    
    rep(i, 0, n) {
        if (d != -1 && s[i] - '0' < d) {
            cout << d;
            d = -1;
        }
        cout << s[i];
    }
    if (d != -1) cout << d;
    cout << '\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 : Conveyor Belts

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() {
    ll n, x1, y1, x2, y2;
    cin >> n >> x1 >> y1 >> x2 >> y2;
    ll  X1 = min(x1, n - x1 + 1),
        Y1 = min(y1, n - y1 + 1),
        X2 = min(x2, n - x2 + 1),
        Y2 = min(y2, n - y2 + 1);
    ll st = min(X1, Y1), en = min(X2, Y2);
    cout << abs(en - st) << '\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 : Restore the Array

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 - 1) cin >> b[i];
    
    a[0] = b[0];
    rep(i, 1, n - 1) a[i] = min(b[i-1], b[i]);
    a[n-1] = b[n-2];
    
    for(auto x : a) cout << x << " ";
    cout << '\n';
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int tc; cin >> tc; while(tc--)
        solve();
    return 0;
}