Codeforces Round #689 (Div. 2, based on Zed Code Competition) 个人题解

1461A. String Generation

void solve() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
        cout << (char)(i < k ? 'a' : 'a' + (i - k) % 3);
    cout << endl;
}

1461B.Find the Spruce

DP,从下往上推

const int N = 500 + 10;

char s[N][N];
int dp[N][N];
void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> s[i] + 1;
    int ans = 0;
    for (int i = n; i; i -= 1)
        for (int j = m; j; j -= 1) {
            dp[i][j] = 0;
            if (s[i][j] == '*') {
                if (i < n and j > 1 and j < m)
                    dp[i][j] = 1 + min({dp[i + 1][j - 1], dp[i + 1][j],
                                        dp[i + 1][j + 1]});
                else
                    dp[i][j] = 1;
            }
            ans += dp[i][j];
        }
    cout << ans << endl;
}

1461C. Random Events

贪心

int a[N];
void solve() {
    cout << fixed << setprecision(6);
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    int m = n;
    while (a[m] == m and m)
        m--;
    double ans = 1, p;
    for (int i = 1, r; i <= q; i += 1) {
        cin >> r >> p;
        if (r >= m)
            ans *= 1 - p;
    }
    if (!m)
        cout << 1.0 << endl;
    else
        cout << 1 - ans << endl;
}

1461D.Divide and Summarize

贪心先把能找到的都找出来, \(O(nlog^2n)\)

int a[N];
ll sum[N];
void solve() {
    cout << fixed << setprecision(6);
    int n, m, q;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    // 前缀和
    for (int i = 1; i <= n; ++i)
        sum[i] = sum[i - 1] + a[i];
    set<ll> s;
    // 用新写法
    function<void(int, int)> DFS = [&](int L, int R) {
        s.insert(sum[R] - sum[L - 1]);
        if (a[L] == a[R])
            return;
        int M = upper_bound(a + L, a + R + 1, (a[L] + a[R]) / 2) - a;
        DFS(L, M - 1),DFS(M, R);
    };
    DFS(1, n);
    for (int i = 1; i <= m; ++i) {
        cin >> q;
        cout << (s.count(q) ? "Yes\n" : "No\n");
    }
}
posted @ 2021-01-04 22:01  RioTian  阅读(100)  评论(0编辑  收藏  举报