Submission #573618

# Submission time Handle Problem Language Result Execution time Memory
573618 2022-06-07T03:03:58 Z SSRS K-th path (IZhO11_kthpath) C++14
100 / 100
1 ms 300 KB
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<char>> A(N, vector<char>(M));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < M; j++){
      cin >> A[i][j];
    }
  }
  long long K;
  cin >> K;
  K--;
  vector<vector<long long>> dp(N, vector<long long>(M, 0));
  dp[N - 1][M - 1] = 1;
  for (int i = N - 1; i >= 0; i--){
    for (int j = M - 1; j >= 0; j--){
      if (i < N - 1){
        dp[i][j] += dp[i + 1][j];
      }
      if (j < M - 1){
        dp[i][j] += dp[i][j + 1];
      }
    }
  }
  string ans;
  ans += A[0][0];
  vector<vector<long long>> dp2(N, vector<long long>(M, 0));
  dp2[0][0] = 1;
  for (int i = 0; i < N + M - 2; i++){
    vector<long long> cnt(26, 0);
    for (int j = max(i + 1 - M, 0); j < min(i + 1, N); j++){
      int k = i - j;
      if (j < N - 1){
        cnt[A[j + 1][k] - 'a'] += dp[j + 1][k] * dp2[j][k];
      }
      if (k < M - 1){
        cnt[A[j][k + 1] - 'a'] += dp[j][k + 1] * dp2[j][k];
      }
    }
    int p = 0;
    for (int j = 0; j < 26; j++){
      if (K >= cnt[j]){
        K -= cnt[j];
        p++;
      } else {
        break;
      }
    }
    ans += 'a' + p;
    for (int j = max(i + 1 - M, 0); j < min(i + 1, N); j++){
      int k = i - j;
      if (j < N - 1){
        if (A[j + 1][k] == 'a' + p){
          dp2[j + 1][k] += dp2[j][k];
        }
      }
      if (k < M - 1){
        if (A[j][k + 1] == 'a' + p){
          dp2[j][k + 1] += dp2[j][k];
        }
      }
    }
  }
  cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct