Submission #993412

# Submission time Handle Problem Language Result Execution time Memory
993412 2024-06-05T14:53:45 Z vjudge1 K-th path (IZhO11_kthpath) C++17
100 / 100
2 ms 444 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 30;
using ll = long long;
ll dp[maxn][maxn][2];
char g[maxn][maxn];
ll ways(int n, int m, string &s) {
    memset(dp, 0, sizeof dp);
    dp[0][0][g[0][0] != s[0]] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i != 0) {
                dp[i][j][1] += dp[i - 1][j][1];
                if (g[i][j] <= s[i + j]) {
                    dp[i][j][g[i][j] != s[i + j]] += dp[i - 1][j][0];
                }
            }
            if (j != 0) {
                dp[i][j][1] += dp[i][j - 1][1];
                if (g[i][j] <= s[i + j]) {
                    dp[i][j][g[i][j] != s[i + j]] += dp[i][j - 1][0];
                }
            }
        }
    }
    return dp[n - 1][m - 1][0] + dp[n - 1][m - 1][1];
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> g[i][j];
        }
    }
    ll k;
    cin >> k;
    string ans{g[0][0]};
    for (int i = 0; i < n + m - 2; i++) {
        int l = -1, hi = 25;
        while (l + 1 < hi) {
            int mid = l + hi >> 1;
            string s = ans + char('a' + mid);
            s.resize(n + m - 1, 'z');
            if (ways(n, m, s) >= k) {
                hi = mid;
            } else {
                l = mid;
            }
        }
        ans += char('a' + hi);
    }
    cout << ans << '\n';

    return 0;
}

Compilation message

kthpath.cpp: In function 'int main()':
kthpath.cpp:43:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |             int mid = l + hi >> 1;
      |                       ~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 2 ms 444 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct