Submission #656826

# Submission time Handle Problem Language Result Execution time Memory
656826 2022-11-08T09:07:36 Z TheEvilBird K-th path (IZhO11_kthpath) C++17
0 / 100
0 ms 324 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)((x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
//typedef __int128_t int128;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const char en = '\n';
const int INF = 1e9 + 7;
const ll INFLL = 1e18;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

#ifdef __APPLE__
#include "debug.h"
//#define debug(...) 42
#else
#define debug(...) 42
#endif

void solve() {
    int n, m;
    cin >> n >> m;
    vector<string> grid(n);
    for (int i = 0; i < n; ++i) {
        cin >> grid[i];
    }
    vector<vector<ll>> dp(n, vector<ll> (m));
    dp[n - 1][m - 1] = 1;
    for (int j = m - 1; j >= 0; --j) {
        dp[n - 1][j] = 1;
    }
    for (int i = n - 1; i >= 0; --i) {
        dp[i][m - 1] = 1;
    }
    for (int i = n - 2; i >= 0; --i) {
        for (int j = m - 2; j >= 0; --j) {
            dp[i][j] = dp[i + 1][j] + dp[i][j + 1];
        }
    }
    ll k;
    cin >> k;
    string ans;
    int i = 0, j = 0;
    ans += grid[i][j];
    while (i < n - 1 && j < m - 1) {
        int v = grid[i][j + 1], u = grid[i + 1][j];
        if (u < v) {
            if (dp[i + 1][j] >= k) {
                ++i;
            } else {
                k -= dp[i + 1][j];
                ++j;
            }
        } else {
            if (dp[i][j + 1] >= k) {
                ++j;
            } else {
                k -= dp[i][j + 1];
                ++i;
            }
        }
        ans += grid[i][j];
    }
    while (i + 1 < n) {
        ++i;
        ans += grid[i][j];
    }
    while (j + 1 < m) {
        ++j;
        ans += grid[i][j];
    }
    cout << ans << en;
}

int main() {
#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -