Codeforces Round 863 (Div. 3)

发布时间 2023-04-05 17:05:24作者: PHarr

A. Insert Digit

放在第一个比他小的数前面

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n, d;
    cin >> n >> d;
    string s;
    cin >> s;
    for (char i: s) {
        if (d > i - '0') cout << d, d = -1;
        cout << i;
    }
    if (d != -1) cout << d;
    cout << "\n";
    return;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    for (; t; t--)
        solve();
    return 0;
}

B. Conveyor Belts

计算两个点分别在第几圈就行了

#include <bits/stdc++.h>

using namespace std;

#define int long long

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

void solve() {
    int n = read(), m = n / 2, a = read(), b = read(), c = read(), d = read();
    if (a > m) a = n - a + 1;
    if (b > m) b = n - b + 1;
    if (c > m) c = n - c + 1;
    if (d > m) d = n - d + 1;
    printf("%lld\n", abs(min(a, b) - min(c, d)));
}

int32_t main() {

    for (int t = read(); t; t--)
        solve();
    return 0;
}

C. Restore the Array

如果\(b_i=b_{i+1}\)则一定满足\(a_{i+1} = b_{i}\),先把这些位置都填完之后再根据条件填入较小值就可以了

#include <bits/stdc++.h>

using namespace std;

#define int long long

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

void solve() {
    int n = read();
    vector<int> a(n + 1, -1), b(n);
    for (int i = 1; i < n; i++) b[i] = read();
    a[1] = b[1], a[n] = b[n - 1];
    for (int i = 2; i < n; i++)
        if (b[i - 1] == b[i]) a[i] = b[i];
        else a[i] = min(b[i - 1], b[i]);
    for (int i = 1; i <= n; i++) printf("%lld ", a[i]);
    printf("\n");
    return;
}

int32_t main() {

    for (int t = read(); t; t--)
        solve();
    return 0;
}

D. Umka and a Long Flight

对于这个矩形满足\(h<w\),所以要减去一个正方形一定是一个\(h\times h\)的,如果这个点在左边就减去右边,如果在右边就对称到左边再减去右边,如果在中间就就寄了。减完之后一定满足\(h>w\),为了方便递归操作可以交换横纵坐标。

#include<bits/stdc++.h>

using namespace std;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

vector<int> fib(46);

bool check( int n , int x , int y ){
    if( n == 0 ) return true;
    if( y > fib[n-1] ) y = fib[n+1] - y + 1;
    if( y > fib[n-1] ) return false;
    return check( n-1 , y , x );
}

int32_t main() {

    fib[0] = fib[1] = 1;
    for( int i = 2 ; i <= 45 ; i ++ ) fib[i] = fib[i-1] + fib[i-2];
    for( int t = read() , n , x , y ; t ; t -- ){
        n = read() , x = read() , y = read();
        printf( check( n , x , y ) ? "YES\n" : "NO\n" );
    }
    return 0;
}

E. Living Sequence

把十进制转换成九进制。如果一位数字\(d\)满足\(d>3\)\(d=d+1\)即可

#include <bits/stdc++.h>

using namespace std;

#define int long long

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

void solve() {
    int n = read();
    vector<int> a;
    while (n) a.push_back(n % 9), n /= 9;
    for (int &i: a)
        if (i > 3) i = i + 1;
    reverse(a.begin(), a.end());
    for( auto i : a )
        cout << i;
    cout << '\n';
    return;
}

int32_t main() {

    for (int t = read(); t; t--)
        solve();
    return 0;
}